2016-10-06 2 views
0

Я добавил ограничения в представления с помощью Interface Builder. Позже в перспективе мне нужно удалить их и добавить новые. Вот как я обновляю ограничение:Objective 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 которая является 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> 
) 

Так в основном, Что я не понимаю, как удалить ограничения постоянно и как обновить представление при добавлении новых.

+0

Если вы удалите все ограничения, возможно, вы не должны добавлять их в IB с самого начала? –

+0

Попробуйте сначала отключить ограничения, а затем применить новые. вы можете просто деактивировать ограничение, установив для свойства 'active' значение' NO'. –

+0

Я не удаляю все ограничения, а 'superView' содержит другие представления рядом с ними. Кроме того, этот сценарий происходит только при выполнении некоторых «редких» условий. –

ответ

0

Создание точек, для ограничений, которые вы хотите удалить. Попробуйте дезактивировать свои ограничения вместо удаления, как показано ниже:

yourconstraint.active = YES; 

После этого назначьте новые ограничения для представления программного и вызовите метод ниже на нужное положение:

[self updateConstraints];

+0

Хорошо, я попробую. –

Смежные вопросы