目标C - 约束在被删除后返回

时间:2016-10-06 10:03:51

标签: ios objective-c autolayout nslayoutconstraint

我使用Interface Builder为视图添加了约束。 稍后在运行中,我需要删除它们并添加新的。 以下是我更新约束的方法:

    for (UIView *view in _fromProduitView.subviews) {
                 [view removeConstraints:view.constraints];
            for (NSLayoutConstraint *constraint in _fromProduitView.constraints) {
                if ([[constraint firstItem] isKindOfClass:[UILabel class]]) {
                    [_fromProduitView removeConstraint:constraint];
                }
            }
     }
     [_fromProduitView addConstraint:[NSLayoutConstraint constraintWithItem:_fonctionLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_fromProduitView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
     [_fromProduitView addConstraint:[NSLayoutConstraint constraintWithItem:_fonctionLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_fromProduitView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:16]];

     [_fromProduitView addConstraint:[NSLayoutConstraint constraintWithItem:_artisanNameLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_fromProduitView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
     [_fromProduitView addConstraint:[NSLayoutConstraint constraintWithItem:_artisanNameLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_fonctionLabel attribute:NSLayoutAttributeBottom multiplier:1.0f constant:8]];

     [_fromProduitView addConstraint:[NSLayoutConstraint constraintWithItem:_supplierAccrocheLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_fromProduitView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
     [_fromProduitView addConstraint:[NSLayoutConstraint constraintWithItem:_supplierAccrocheLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_artisanNameLabel attribute:NSLayoutAttributeBottom multiplier:1.0f constant:8]];

此时,我记录了视图_fromProductView的限制,这是超级视图:

<__NSArrayI 0x7fdc2df84cc0>(
<NSLayoutConstraint:0x7fdc2dfa2d40 UIView:0x7fdc2dfa59e0.height == 0>,
<NSLayoutConstraint:0x7fdc2db32320 UILabel:0x7fdc2dfa4730.centerX == UIView:0x7fdc2dfa59e0.centerX>,
<NSLayoutConstraint:0x7fdc2e9f3210 UILabel:0x7fdc2dfa4730.top == UIView:0x7fdc2dfa59e0.leading + 16>,
<NSLayoutConstraint:0x7fdc2df84960 UILabel:0x7fdc2dfb0d60.centerX == UIView:0x7fdc2dfa59e0.centerX>,
<NSLayoutConstraint:0x7fdc2cff0b90 UILabel:0x7fdc2dfb0d60.top == UILabel:0x7fdc2dfa4730.bottom + 8>,
<NSLayoutConstraint:0x7fdc2e9f38b0 UILabel:0x7fdc2dfa4c30.centerX == UIView:0x7fdc2dfa59e0.centerX>,
<NSLayoutConstraint:0x7fdc2e9f4390 UILabel:0x7fdc2dfa4c30.top == UILabel:0x7fdc2dfb0d60.bottom + 8>
)

这是我想要的方式,但问题是,后来我删除的限制会回来并与我创造的那些相冲突,导致意见消失:

<__NSArrayI 0x7fdc2b5f7460>(
<NSLayoutConstraint:0x7fdc2dfa2d40 UIView:0x7fdc2dfa59e0.height == 0>,
<NSLayoutConstraint:0x7fdc2e9f3210 UILabel:0x7fdc2dfa4730.top == UIView:0x7fdc2dfa59e0.leading + 16>,
<NSLayoutConstraint:0x7fdc2df84960 UILabel:0x7fdc2dfb0d60.centerX == UIView:0x7fdc2dfa59e0.centerX>,
<NSLayoutConstraint:0x7fdc2e9f38b0 UILabel:0x7fdc2dfa4c30.centerX == UIView:0x7fdc2dfa59e0.centerX>,
<NSLayoutConstraint:0x7fdc2dfa2de0 UILabel:0x7fdc2dfa4730.centerX == UIView:0x7fdc2dfa59e0.centerX - 8>,
<NSLayoutConstraint:0x7fdc2dfa2e30 UILabel:0x7fdc2dfa4730.leading == UILabel:0x7fdc2dfb0d60.leading>,
<NSLayoutConstraint:0x7fdc2dfa2ed0 UILabel:0x7fdc2dfb0d60.top == UILabel:0x7fdc2dfa4730.bottom + 2>,
<NSLayoutConstraint:0x7fdc2dfa2f70 UILabel:0x7fdc2dfa4c30.leading == UILabel:0x7fdc2dfb0d60.leading>,
<NSLayoutConstraint:0x7fdc2dfa2fc0 UILabel:0x7fdc2dfa4c30.top == UILabel:0x7fdc2dfb0d60.bottom>,
<NSLayoutConstraint:0x7fdc2cff0b90 UILabel:0x7fdc2dfb0d60.top == UILabel:0x7fdc2dfa4730.bottom + 8>,
<NSLayoutConstraint:0x7fdc2e9f4390 UILabel:0x7fdc2dfa4c30.top == UILabel:0x7fdc2dfb0d60.bottom + 8>,
<NSLayoutConstraint:0x7fdc2db32320 UILabel:0x7fdc2dfa4730.centerX == UIView:0x7fdc2dfa59e0.centerX>
)

所以主要是,我不明白如何永久删除约束以及如何在添加新视图时更新视图

1 个答案:

答案 0 :(得分:0)

为要删除的约束创建出口。尝试停用约束,而不是删除如下:

yourconstraint.active = YES;

之后,以编程方式将新约束分配给视图,并在所需视图上调用以下方法:

[self updateConstraints];