我的 nib 文件中有 2 个 View (X 和 Y)。我为此使用了自动布局。
我的第二个 View (Y)是底部空间是 0 View 意味着它在主视图的底部。 我的第一个 View (x) 是底部空间是 0 到 View(Y) 表示 X 和 Y 相互连接。
如果我从编码中删除 View (Y),那么 View (X) 应该被放置,因为 View (y) 意味着 View (X) 应该向下到 mani View 底部。
我的尝试如下:
[vwOperation2 removeFromSuperview];
NSLog(@"%@",vwOperation2.constraints);
vwOperation2.translatesAutoresizingMaskIntoConstraints = NO;
[vwOperation2 updateConstraints];
vwOperation1.translatesAutoresizingMaskIntoConstraints = NO;
[vwOperation1 updateConstraints];
[self.view setNeedsLayout];
帮我解决这个问题..
提前致谢...
最佳答案
切勿直接调用 updateConstraints
。
而是使用 setNeedsUpdateConstraints
和 needsUpdateConstraints
。
关于问题。
在 UIView
的 updateConstraints
方法中添加您需要考虑 View 状态的约束。例如:
- (void)updateConstraints {
// You can remove all the constraints here are only some of them
// You may have some IBOutlets for constraints
[self removeConstraints:self.constraints];
if (self.shouldHideX) {
// add proper constrains here
// or modify constants
} else {
// add proper constrains here
// or modify constants
}
[super updateConstraints];
}
然后在您的代码中触发更改,您应该执行如下操作:
- (void)doSomethingAwesome {
self.shouldHideX = // determine that
[self.view setNeedsUpdateConstraints];
[self.view setNeedsLayout];
// if you want to animate that
[UIView animateWithDuration:0.2
animations:^{
[self needsUpdateConstraints];
[self layoutIfNeeded];
} completion:^(BOOL finished){
// here e.g. remove your subview from superview
}];
}
关于ios - 更改 Iphone 中的垂直约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23293328/