自动布局正在做一些对我来说没有意义的事情,但希望对你们中的一个人来说:
我有一个 UIView containerView 和 5 个 UIViews coverview1-coverview5。我想在 containerView 中将这 5 个封面视图彼此相邻放置:相同的宽度,相同的底部对齐,任何地方都没有间距,例如 |[ __ ][ __ ][ __ ][ __ ][ __ ]|
所以我在做:
[coverviewContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
@"H:|[coverview1(>=0)][coverview2(==coverview1)][coverview3(==coverview1)][coverview4(==coverview1)][coverview5(==coverview1)]|" options:NSLayoutFormatAlignAllBottom metrics:nil views:viewsDictionary]];
我得到:
Unable to simultaneously satisfy constraints.
(
"<NSLayoutConstraint:0x1d5ae4f0 ItemView:0x1d58f6f0.centerX == ItemView:0x1d580410.centerX>",
"<NSLayoutConstraint:0x1d5ae470 ItemView:0x1d58f6f0.centerX == ItemView:0x1d59b480.centerX>",
"<NSLayoutConstraint:0x1d5aed70 ItemView:0x1d580410.centerX == ItemView:0x1d59b480.centerX>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1d5ae470 ItemView:0x1d58f6f0.centerX == ItemView:0x1d59b480.centerX>
奇怪的是,如果我执行以下任何操作,我都不会收到警告:
[coverviewContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
@"H:|[coverview1(>=0)]|" options:NSLayoutFormatAlignAllBottom metrics:nil views:viewsDictionary]];
//or
[coverviewContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
@"H:|[coverview1(>=0)][coverview2(==coverview1)]|" options:NSLayoutFormatAlignAllBottom metrics:nil views:viewsDictionary]];
//or
[coverviewContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
@"H:|[coverview1(>=0)][coverview2(==coverview1)][coverview3(==coverview1)]|" options:NSLayoutFormatAlignAllBottom metrics:nil views:viewsDictionary]];
//or
[coverviewContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
@"H:|[coverview1(>=0)][coverview2(==coverview1)][coverview3(==coverview1)][coverview4(==coverview1)]|" options:NSLayoutFormatAlignAllBottom metrics:nil views:viewsDictionary]];
//or
[coverviewContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
@"H:|[coverview1(>=0)][coverview2(==coverview1)][coverview3(==coverview1)][coverview5(==coverview1)]|" options:NSLayoutFormatAlignAllBottom metrics:nil views:viewsDictionary]];
(最后两个不一样,因为我把coverview4换成了coverview5)
为什么我最多只能添加 4 个视图?为什么添加第 5 个会导致视图 centerX 对齐突然出现问题?