Jac*_*son 7 iphone objective-c ios autolayout
关于如何正确定义约束,在删除给定视图时会折叠视图之间的空间,我感到有点难过.我尝试添加大于或等于关系的垂直约束,但是,似乎不满足约束的最小距离.
例如,给定以下具有三个约束V的布局:[A] -5- [B],V:[B] -5- [C],并且V:[A] - (> = 5) - [C] :
[ View A ]
|
5 pt
|
[ View B ]
|
5 pt
|
[ View C ]
Run Code Online (Sandbox Code Playgroud)
删除视图后,BI希望它看起来像这样:
[ View A ]
|
5 pt
|
[ View C ]
Run Code Online (Sandbox Code Playgroud)
但它看起来像这样:
[ View A ]
|
5 pt + 5 pt + height of view B
|
[ View C ]
Run Code Online (Sandbox Code Playgroud)
您可以添加V:[A] -5- [C],优先级低于1000.
[superview]
|
[ View A ]
| |
5 pt (1000) |
| |
[ View B ] 5 pt (999)
| |
5 pt (1000) |
| |
[ View C ]
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找可扩展性,您可能必须用代码来完成。我用类似的东西
UIView *superView = /* whatever the superview of your views is, probably self.view in a lot of cases */
NSArray *arrViews = [NSArray arrayWithObjects:/* put only the things you want to show in here, do not put the hidden things, and put them in order from top to bottom */, nil];
CGFloat buffer = 5;
[superView addConstraint:[NSLayoutConstraint constraintWithItem:[arrViews objectAtIndex:0] attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1 constant:buffer]];
for (int i = 1; i < arrViews.count; i++)
{
[superView addConstraint:[NSLayoutConstraint constraintWithItem:[arrViews objectAtIndex:i] attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:[arrViews objectAtIndex:i-1] attribute:NSLayoutAttributeBottom multiplier:1 constant:buffer]];
}
[superView addConstraint:[NSLayoutConstraint constraintWithItem:[arrViews lastObject] attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1 constant:-1*(buffer)]];
Run Code Online (Sandbox Code Playgroud)
buffer这将在顶部项目和它的超级视图之间,然后在每个子视图和它正上方的子视图之间,然后在底部视图和超级视图之间放置一个固定的间距(大小 = )。每次从 中删除项目时都必须调用此函数arrViews,然后调用[superView needsLayout]。您还需要确保以某种方式设置高度限制,否则您会收到错误。如果所有东西都具有相同的高度,您可以在循环中添加另一行来添加高度约束。
| 归档时间: |
|
| 查看次数: |
1325 次 |
| 最近记录: |