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

哈伦·斯米达(Haroun SMIDA)

我已经使用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即superview:

<__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>
)

所以主要来说,我不了解的是如何永久删除约束以及添加新约束时如何更新视图

维克拉姆·帕里米(Vikram Parimi)

为要删除约束的出口创建出口。尝试取消激活约束,而不是像下面这样删除:

yourconstraint.active = YES;

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

[self updateConstraints];

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章