2015-06-03 37 views
13

I wprowadziły autoLayout programowo przy użyciu kodu:Jak zmienić lub zaktualizować NSLayoutConstraint programowo

- (void)addConstraintWithListNavigationViewController:(UIView *)listViewNavigation y:(CGFloat)y height:(CGFloat)height 
{ 
    //WIDTH_ListTableView = 0.4 

    //set x = 0; 
    NSLayoutConstraint *constraintToAnimate1 = [NSLayoutConstraint constraintWithItem:listViewNavigation 
                      attribute:NSLayoutAttributeLeft 
                      relatedBy:NSLayoutRelationEqual 
                       toItem:self.view 
                      attribute:NSLayoutAttributeLeft 
                      multiplier:0.00 
                      constant:0]; 
    [self.view addConstraint:constraintToAnimate1]; 


    //set y = y; 
    NSLayoutConstraint *constraintToAnimate2 = [NSLayoutConstraint constraintWithItem:listViewNavigation 
                      attribute:NSLayoutAttributeTop 
                      relatedBy:NSLayoutRelationEqual 
                       toItem:self.view 
                      attribute:NSLayoutAttributeBottom 
                      multiplier:0.00 
                      constant:y]; 
    [self.view addConstraint:constraintToAnimate2]; 








    //set Width = self.view.frame.size.width*0.4 
    NSLayoutConstraint *constraintToAnimate3 = [NSLayoutConstraint constraintWithItem:listViewNavigation 
                      attribute:NSLayoutAttributeWidth 
                      relatedBy:NSLayoutRelationEqual 
                       toItem:self.view 
                      attribute:NSLayoutAttributeWidth 
                      multiplier:1-WIDTH_ListTableView 
                      constant:0.0]; 
    [self.view addConstraint:constraintToAnimate3]; 





    //Set height = height 
    NSLayoutConstraint *constraintToAnimate4 = [NSLayoutConstraint constraintWithItem:listViewNavigation 
                      attribute:NSLayoutAttributeHeight 
                      relatedBy:NSLayoutRelationEqual 
                       toItem:nil 
                      attribute:NSLayoutAttributeNotAnAttribute 
                      multiplier:0.00 
                      constant:height]; 
    [self.view addConstraint:constraintToAnimate4]; 
} 

I to działa idealnie, ale każdy czas ten ViewController odbiera Notification, to będzie działać:

[self.view layoutIfNeeded]; 

Ale chcę ustawić szerokość listyViewView zgodnie z połączoną zmienną logiczną.

if(connected){ 
     listViewNavigation.view.frame.size.width = 0.4 * self.view.frame.size.width; 
    } 
    else{ 
     listViewNavigation.view.frame.size.width = 0.6 * self.view.frame.size.width; 
    } 

Ale nie wiem, jak mogę zaktualizować NSLayoutConstraint:

NSLayoutConstraint *constraintToAnimate3 = [NSLayoutConstraint constraintWithItem:PreView 
                      attribute:NSLayoutAttributeWidth 
                      relatedBy:NSLayoutRelationEqual 
                       toItem:self.view 
                      attribute:NSLayoutAttributeWidth 
                      multiplier:1 - WIDTH_ListTableView 
                      constant:0.0]; 
[self.view addConstraint:constraintToAnimate3]; 

gdy ViewController otrzymać powiadomienie.

Odpowiedz

11

OK, domyślam się.

[self.view removeConstraint:self.constraintOld]; 
[self.view addConstraint:self.constraintNew]; 

[UIView animateWithDuration:time animations:^{ 
    [self.view layoutIfNeeded]; 
}]; 
+0

To jest najlepszy sposób na zrobienie tego, nawet jeśli edytujesz całkowicie to samo ograniczenie. –

15

Myślę, że masz 2 opcje.

Wariant 1 Przechowywać właściwość

@property (strong,nonatomic)NSLayoutConstraint *constraintToAnimate3; 

Następnie za pomocą tej właściwości

self.constraintToAnimate3 = [NSLayoutConstraint constraintWithItem:PreView 
                     attribute:NSLayoutAttributeWidth 
                     relatedBy:NSLayoutRelationEqual 
                      toItem:self.view 
                     attribute:NSLayoutAttributeWidth 
                     multiplier:1 
                     constant:-1 * 0.4 * self.view.frame.size.width]; 
[self.view addConstraint:self.constraintToAnimate3]; 

Gdy chcesz zmienić

if(connected){ 
    self.constraintToAnimate3.constant = -1 *0.6 * self.view.frame.size.width; 
} 
else{ 
    self.constraintToAnimate3.constant = -1 *0.4 * self.view.frame.size.width; 
} 
[UIView animateWithDuration:yourduration animations:^{ 
    [self.view layoutIfNeeded]; 
}]; 

Opcja 2 Ustaw identyfikator constraintToAnimate3

constraintToAnimate3.identifier = @"1234" 

Następnie wyszukaj dostać ograniczenie

NSLayoutConstraint * constraint3 = nil; 
NSArray * constraints = self.view.constraints; 
for (NSLayoutConstraint * constraint in constraints) { 
    if ([constraint.identifier isEqualToString:@"1234"]) { 
     constraint3 = constraint; 
     break; 
    } 
} 

następnie zmienić stałą, jak pokazano na opcja1

Aktualizacja: Jeśli użycie stałe w kodzie I po

PreView.frame.size.with = self.view.size.width * mnożnik + stała

+0

Opcja 1 nie działa, czy możesz sprawdzić? – Albert

+0

W ten sposób musisz zaktualizować constant.So zmieniam ograniczenie na "mnożnik: 1, stały: 0.4 * self.view.frame.size.width", możesz ustawić stałą na odpowiednią stałą, tak jak lubisz – Leo

+0

ListViewNavigation.frame.size.width jest zbyt duży. Jeśli ustawię mnożnik = 0, to jest OK. Ale pamiętam, że Apple nie pozwala ustawić mnożnika = 0; – Albert

0

Jest to istotne, że nie tylko dodawać ograniczenia do widzenia, ale również je zapamiętać.

Najłatwiej jest zmienić wiązanie, jeśli wystarczy zmienić jego stałą - stała jest jedyną częścią wiązania, która może być później wielokrotnie zmieniana. Aby to zrobić, musisz zapisać wiązanie samodzielnie.

W przeciwnym razie należy usunąć stare więzy i dodać nowe. Ponieważ zwykle masz więcej niż jedno ograniczenie, przechowuj tablice z zestawami ograniczeń, które mogą wymagać wymiany, a następnie zaktualizuj całą tablicę. Możesz także użyć metod activateConstraints i deaktywujConstraints.