Closed shenguanjiejie closed 7 years ago
但是好像masonry是基于系统的NSLayoutConstraint实现的 我同事用masonry约束的cell就可以正常使用SDAutoLayout...
@gsdios 你好 可以回答一下我的问题吗 谢谢 我最近又要用到 很想用 但因为习惯原因 又很想用系统layout 是不可行的么?
具体怎么用的呢,代码贴出来下
` _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的,所以就不清楚有什么区别了,但是问题应该就是出在这里.
...我可能忘了说明一点了 我只是要用自动计算cell高度的功能 所以可能上面没说清楚导致了误解
sd是基于frame的自主设计算法实现自动布局,不要和系统的约束混用