2016-10-06 7 views
0

Interface Builderを使用してビューに制約を追加しました。 後で実行するには、それらを削除して新しいものを追加する必要があります。ここで は私が制約に更新する方法です。この時点で目的C - 削除後に制約が返される

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

私が好きな方法であるが、問題があります、後で私が削除したcontraintsは、私はビューが消えるまでに結果を作成しましたものに戻ってくると紛争:

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

ので、主に、どのようなI永続的に制約を削除する方法と、新しいものが追加されたときにビューを更新する方法です。

+0

すべての制約を削除すると、最初からIBに追加しないでください。 –

+0

最初に制約を無効にしてから新しい制約を適用してください。 'active'プロパティを' NO'に設定することで制約を単純に無効にすることができます。 –

+0

私はすべての制約を取り除くつもりではなく、 'superView'はそれらの他のビューを含んでいます。さらに、このシナリオは、いくつかの「まれな」条件が満たされた場合にのみ発生します。 –

答えて

0

削除する制約のアウトレットを作成します。代わりに以下のように削除あなたの制約を無効にしてみてください。その後

yourconstraint.active = YES; 

プログラムでビューに新しい制約を割り当てて、必要なビューに以下のメソッドを呼び出す:

[self updateConstraints]を。

+0

さて、私はそれを試してみましょう。 –

関連する問題