gpt4 book ai didi

ios - 为什么这个布局模棱两可?

转载 作者:行者123 更新时间:2023-11-29 02:47:28 25 4
gpt4 key购买 nike

我有一个带有一些标签和 ImageView 的基本单元格。我使用视觉格式语言添加约束,但在运行时它们变得模棱两可。这是怎么回事?!

[self.constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[_typeLabel]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_typeLabel)]];
[self.constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[_addressLabel]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_addressLabel)]];
[self.constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[_mapImageView]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_mapImageView)]];
[self.constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-11-[_typeLabel]-2-[_addressLabel]-[_mapImageView]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_typeLabel, _addressLabel, _mapImageView)]];

这是我的 _autolayoutTrace:

*<UITableViewCellContentView:0x940dc30>
| *<UILabel:0x943aab0> - AMBIGUOUS LAYOUT
| *<UILabel:0x940ead0> - AMBIGUOUS LAYOUT
| *<UIImageView:0x9445c20> - AMBIGUOUS LAYOUT
<_UITableViewCellSeparatorView:0x9446200>

下面是影响前两个 UILabel 的约束:

(lldb) po [0x943aab0 constraintsAffectingLayoutForAxis:0]
<__NSArrayM 0x975fee0>(
<NSLayoutConstraint:0x940eca0 H:|-(15)-[UILabel:0x943aab0] (Names:'|':UITableViewCellContentView:0x940dc30 )>,
<NSLayoutConstraint:0x940ecf0 H:[UILabel:0x943aab0]-(10)-| (Names:'|':UITableViewCellContentView:0x940dc30 )>,
<NSAutoresizingMaskLayoutConstraint:0x9446a70 h=--& v=--& H:[UITableViewCellContentView:0x940dc30(320)]>
)


(lldb) po [0x943aab0 constraintsAffectingLayoutForAxis:1]
<__NSArrayM 0xaa4af10>(
<NSLayoutConstraint:0x9446030 V:|-(11)-[UILabel:0x943aab0] (Names:'|':UITableViewCellContentView:0x940dc30 )>,
<NSContentSizeLayoutConstraint:0x943a840 V:[UILabel:0x943aab0(17)] Hug:250 CompressionResistance:750>
)


(lldb) po [0x940ead0 constraintsAffectingLayoutForAxis:0]
<__NSArrayM 0x97b4240>(
<NSLayoutConstraint:0x940ed90 H:|-(15)-[UILabel:0x940ead0] (Names:'|':UITableViewCellContentView:0x940dc30 )>,
<NSLayoutConstraint:0x940ede0 H:[UILabel:0x940ead0]-(10)-| (Names:'|':UITableViewCellContentView:0x940dc30 )>,
<NSAutoresizingMaskLayoutConstraint:0x9446a70 h=--& v=--& H:[UITableViewCellContentView:0x940dc30(320)]>
)


(lldb) po [0x940ead0 constraintsAffectingLayoutForAxis:1]
<__NSArrayM 0xa985d30>(
<NSLayoutConstraint:0x9446030 V:|-(11)-[UILabel:0x943aab0] (Names:'|':UITableViewCellContentView:0x940dc30 )>,
<NSLayoutConstraint:0x9446080 V:[UILabel:0x943aab0]-(2)-[UILabel:0x940ead0]>,
<NSContentSizeLayoutConstraint:0x943a840 V:[UILabel:0x943aab0(17)] Hug:250 CompressionResistance:750>,
<NSContentSizeLayoutConstraint:0x9446bb0 V:[UILabel:0x940ead0(33)] Hug:250 CompressionResistance:750>
)

最佳答案

对于模棱两可的布局,您需要设置标签的属性,例如 contentHuggingcontentCompressionResistance

希望对你有帮助

编辑: 要了解此属性,您可以关注 Cocoa Autolayout: content hugging vs content compression resistance priority

关于ios - 为什么这个布局模棱两可?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24929291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com