我有一个UIViewController,我想根据一个按钮的点击动态地修改它的一些布局。
作为一个简化的例子,假设我在IB (self.box)中创建了self.box。在IB中,设置一个约束,使视图的宽度等于VC视图宽度的0.9。
然后以编程方式添加另一个约束,并将其保存为强属性。它将视图的宽度设置为VC视图宽度的0.7,并删除前面的约束。
这将导致冲突的约束错误和一个完全破坏的视图。无论是先添加还是删除,还是使用.active属性而不是添加/删除,都会发生同样的情况。
知道该怎么做吗?我的实际情况要复杂得多,但我已经检查过了,而且只有在UIViewController中修改视图的宽度约束才会导致错误,尽管将其设置为IB中的不同乘数不会导致任何冲突。
- (void) viewDidLoad
{
NSLayoutConstraint* lowWidth = [NSLayoutConstraint constraintWithItem:self.box attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:0.7 constant:0.f];
self.lowWidth = lowWidth;
[self.view removeConstraint:self.highWidth];
[self.view addConstraint:self.lowWidth];
}约束冲突包括以下内容:
Probably at least one of the constraints in the following list is one you don't want.
Try this:
<NSLayoutConstraint:0x145efb0f0 UIView:0x145d8d330.width == 0.7*UIView:0x14755e890.width>,
<NSLayoutConstraint:0x1474589b0 UIView:0x145d8d330.width == 0.9*UIView:0x14755e890.width>编辑:调用setActive: outlet约束似乎不起作用,因此我删除了出口并以编程方式创建了约束。这一次,它发挥了预期的作用,但我无法解释为什么出口没有发挥作用。请注意,设置为弱/强并不会改变任何事情。
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *highWidth;发布于 2016-02-25 12:44:43
我只使用一个NSLayoutConstraint并修改约束大小。
self.highWidth?.contant = size
[self.view layoutIfNeeded]或者激活/解除约束..。NSLayoutConstraint类
https://stackoverflow.com/questions/35614624
复制相似问题