如何将 2 个动态宽度的 UILabel 放置到具有 Masonry 约束的视图中?
How to place 2 dynamic width UILabel to a view with Masonry constraints?
我真的很喜欢 Masonry 图书馆。它使我免于 Apple Autolayout 的冲突。
我用这个库解决了大部分问题。但是有一件事我没能做到。
同一个 UIView 中有 2 个标签。两个标签都有动态宽度。我写了这段代码,但出现了问题
[self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.bottomContainer.centerY);
make.left.equalTo(self.left).with.offset(@10);
make.width.lessThanOrEqualTo(@120);
make.leading.equalTo(self.priceLabel.trailing);
}];
[self.priceLabel makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.bottomContainer.centerY);
make.right.equalTo(self.right).with.offset(@-10);
make.width.lessThanOrEqualTo(@150);
}];
我应该怎么做才能避免此类问题?
我用这段代码解决了我的问题:
[self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.bottomContainer.centerY);
make.left.equalTo(self.left).with.offset(@10);
make.trailing.equalTo(self.priceLabel.leading);
}];
[self.priceLabel makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.bottomContainer.centerY);
make.right.equalTo(self.right).with.offset(@-10);
}];
[self.priceLabel setContentCompressionResistancePriority: UILayoutPriorityDefaultHigh forAxis: UILayoutConstraintAxisHorizontal];
[self.nameLabel setContentCompressionResistancePriority: UILayoutPriorityDefaultLow forAxis: UILayoutConstraintAxisHorizontal];
谢谢
我真的很喜欢 Masonry 图书馆。它使我免于 Apple Autolayout 的冲突。
我用这个库解决了大部分问题。但是有一件事我没能做到。
同一个 UIView 中有 2 个标签。两个标签都有动态宽度。我写了这段代码,但出现了问题
[self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.bottomContainer.centerY);
make.left.equalTo(self.left).with.offset(@10);
make.width.lessThanOrEqualTo(@120);
make.leading.equalTo(self.priceLabel.trailing);
}];
[self.priceLabel makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.bottomContainer.centerY);
make.right.equalTo(self.right).with.offset(@-10);
make.width.lessThanOrEqualTo(@150);
}];
我应该怎么做才能避免此类问题?
我用这段代码解决了我的问题:
[self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.bottomContainer.centerY);
make.left.equalTo(self.left).with.offset(@10);
make.trailing.equalTo(self.priceLabel.leading);
}];
[self.priceLabel makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.bottomContainer.centerY);
make.right.equalTo(self.right).with.offset(@-10);
}];
[self.priceLabel setContentCompressionResistancePriority: UILayoutPriorityDefaultHigh forAxis: UILayoutConstraintAxisHorizontal];
[self.nameLabel setContentCompressionResistancePriority: UILayoutPriorityDefaultLow forAxis: UILayoutConstraintAxisHorizontal];
谢谢