我有一系列以这种方式堆叠的视图
________________
| |
| View 1 |
|________________|
| |
| View 2 |
|________________|
| |
| View 3 |
|________________|
这些视图可以展开和折叠,因此如果展开视图 1,则视图 2 的顶部位于视图 1 的底部,与视图 2 相关的视图 3 也是如此。
________________
| |
| View 1 |
| |
| |
| |
|________________|
| |
| View 2 |
|________________|
| |
| View 3 |
|________________|
________________
| |
| View 1 |
|________________|
| |
| View 2 |
| |
| |
| |
|________________|
| |
| View 3 |
|________________|
我不能通过 IB 添加这些视图,因为这个布局是动态创建的,所以我也必须通过代码和约束添加视图。
我正在做这个
UIView *previousView = nil;
for (UIView *view in views) {
if (previousView) {
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[previousView][view]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(previousView, view)];
[superview addConstraints:constraints];
}
}
当我点击视图以展开它时,我收到一个错误
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x76407b0 h=&-& v=--& V:[MyView:0x764c0f0(44)]>",
"<NSAutoresizingMaskLayoutConstraint:0x763e370 h=&-& v=--& V:[MyView:0x7646490(44)]>",
"<NSLayoutConstraint:0x76440d0 V:[MyView:0x764c0f0]-(0)-[MyView:0x7648eb0]>",
"<NSLayoutConstraint:0x7643920 V:[MyView:0x7648eb0]-(0)-[MyView:0x7646490]>",
"<NSAutoresizingMaskLayoutConstraint:0x76407f0 h=&-& v=--& MyView:0x764c0f0.midY == + 22>",
"<NSAutoresizingMaskLayoutConstraint:0x76d9ea0 h=&-& v=--& MyView:0x7648eb0.midY == + 91.5>",
"<NSAutoresizingMaskLayoutConstraint:0x763e3b0 h=&-& v=--& MyView:0x7646490.midY == + 110>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7643920 V:[MyView:0x7648eb0]-(0)-[MyView:0x7646490]>
显然我做错了。我是否必须设置并将我自己的约束添加到定位setTranslatesAutoresizingMaskIntoConstraints
?NO
或者问题是我在那个循环上添加的约束?