gsdios / SDAutoLayout

One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime.
MIT License
5.9k stars 1.28k forks source link

...不支持VFL么? 我用VFL布局,不能够正确计算cell高度,改成masonry约束就可以 明明是一样的约束 #171

Closed shenguanjiejie closed 7 years ago

gsdios commented 7 years ago

sd是基于frame的自主设计算法实现自动布局,不要和系统的约束混用

shenguanjiejie commented 7 years ago

但是好像masonry是基于系统的NSLayoutConstraint实现的 我同事用masonry约束的cell就可以正常使用SDAutoLayout...

shenguanjiejie commented 7 years ago

@gsdios 你好 可以回答一下我的问题吗 谢谢 我最近又要用到 很想用 但因为习惯原因 又很想用系统layout 是不可行的么?

gsdios commented 7 years ago

具体怎么用的呢,代码贴出来下

shenguanjiejie commented 7 years ago

` _iconView = [[UIImageView alloc]init]; _iconView.translatesAutoresizingMaskIntoConstraints = NO; _iconView.contentMode = UIViewContentModeScaleAspectFit; [self.contentView addSubview:_iconView];

    _titleLab = [[UILabel alloc]init];
    _titleLab.translatesAutoresizingMaskIntoConstraints = NO;
    _titleLab.font = [UIFont systemFontOfSize:15];
    _titleLab.textColor = kGrayColor;
    [self.contentView addSubview:_titleLab];

    _progressLab = [[UILabel alloc]init];
    _progressLab.translatesAutoresizingMaskIntoConstraints = NO;
    _progressLab.font = [UIFont systemFontOfSize:15];
    _progressLab.textColor = kLightGrayColor;
    [self.contentView addSubview:_progressLab];

    _dateLab = [[UILabel alloc]init];
    _dateLab.translatesAutoresizingMaskIntoConstraints = NO;
    _dateLab.font = [UIFont systemFontOfSize:12];
    _dateLab.textColor = kLightGrayColor;
    [self.contentView addSubview:_dateLab];

    _statusLab = [[UILabel alloc]init];
    _statusLab.translatesAutoresizingMaskIntoConstraints = NO;
    _statusLab.font = [UIFont systemFontOfSize:15];
    _statusLab.textColor = kGreenColor;
    _statusLab.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_statusLab];

    UIView *lineView = [[UIView alloc]init];
    lineView.translatesAutoresizingMaskIntoConstraints = NO;
    lineView.backgroundColor = kGlobalBackgroundColor;
    [self.contentView addSubview:lineView];

    NSDictionary *views = NSDictionaryOfVariableBindings(_iconView,_titleLab,_progressLab,_dateLab,_statusLab,lineView);
    NSDictionary *metrics = @{@"margin":@(getWidth(20)),@"iconWH":@(getWidth(100))};
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-margin-[_iconView(iconWH)]-margin-[_titleLab]->=0-[_statusLab]-margin-|" options:0 metrics:metrics views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-margin-[_titleLab][_progressLab(_titleLab)][_dateLab(_titleLab)]-margin-|" options:NSLayoutFormatAlignAllLeft metrics:metrics views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-margin-[_statusLab(_titleLab)]" options:0 metrics:metrics views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[lineView]|" options:0 metrics:nil views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[lineView(4)]|" options:0 metrics:nil views:views]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_iconView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_iconView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:getWidth(100)]];`

这是我设置的一个UITableViewCell的layout,没有用第三方的框架,全是用的系统自带layout,然后按照SDAutoLayout标准设置自动计算cell高度,无效. 后来,我同事在不改变其他代码的情况下,只将我写的layout使用Masonry重写了一遍,就能正常计算了,然后我去看Masonry源码,发现Masonry的layout实现是继承自系统layout的,所以就不清楚有什么区别了,但是问题应该就是出在这里.

shenguanjiejie commented 7 years ago

...我可能忘了说明一点了 我只是要用自动计算cell高度的功能 所以可能上面没说清楚导致了误解