5

Mam kontroler widoku ujęty w storyboard z włączonym autolayout i szukam sposobu na zmianę ograniczeń, aby mój widok mógł się obracać w krajobraz i zmieniać kolejność przycisków na ekranie. Kiedy wypróbuję poniższy kod, otrzymuję około dwóch tuzinów "niezdolnych do spełnienia ograniczeń, łamanie ograniczeń ...", których naprawdę nie mogę rozszyfrować.UIStoryboard jak programowo zastąpić ograniczenia?

Czy istnieje sposób dynamicznego zastępowania wiązań z serii ujęć z ograniczeniami, które określam programowo? Chcę całkowicie zastąpić układ przycisków, które zdefiniowałem w scenorysie.

-(void)updateViewConstraints 
{ 
    [super updateViewConstraints]; 

    self.loginButton.translatesAutoresizingMaskIntoConstraints = NO; 
    self.getStartedButton.translatesAutoresizingMaskIntoConstraints = NO; 
    self.takeTourButton.translatesAutoresizingMaskIntoConstraints = NO; 



    [self.loginButton removeConstraints:self.loginButton.constraints]; 
    [self.getStartedButton removeConstraints:self.getStartedButton.constraints]; 
    [self.takeTourButton removeConstraints:self.takeTourButton.constraints]; 


    one = self.loginButton; 
    two = self.getStartedButton; 
    three = self.takeTourButton; 


    NSDictionary *metrics = @{@"height":@50.0,@"width":@100.0,@"leftSpacing":@110.0}; 
    NSDictionary *views = NSDictionaryOfVariableBindings(one,two,three); 

    [self.view removeConstraints:activeTestLabelConstraints]; 
    [activeTestLabelConstraints removeAllObjects]; 

    if(isRotatingToLandscape) 
    { 

     [self registerConstraintsWithVisualFormat:@"|-[one(two)]-[two(three)]-[three]-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metrics views:views]; 
     [self registerConstraintsWithVisualFormat:@"V:[one(height)]-|" options:0 metrics:metrics views:views]; 

    }else 
    { 

     [self registerConstraintsWithVisualFormat:@"|-leftSpacing-[one(width)]" options:0 metrics:metrics views:views]; 
     [self registerConstraintsWithVisualFormat:@"[two(width)]" options:0 metrics:metrics views:views]; 
     [self registerConstraintsWithVisualFormat:@"[three(width)]" options:0 metrics:metrics views:views]; 
     [self registerConstraintsWithVisualFormat:@"V:[one(height)]-[two(75)]-[three(100)]-|" options:NSLayoutFormatAlignAllCenterX metrics:metrics views:views]; 
    } 


} 

Zaktualizowano odpowiedź Roba, oto metoda na usunięcie ograniczeń, które używam

-(void)removeConstraintForView:(UIView*)viewToModify 
{ 

    UIView* temp = viewToModify; 
    [temp removeFromSuperview]; 
    [self.view addSubview:temp]; 

} 

Odpowiedz

10

Brzmi jak chcesz po prostu usunąć wszystkie ograniczenia na widoku. Ponieważ ograniczenia na widok często są w posiadaniu przodka widoku, nie jest oczywiste, jak łatwo usunąć wszystkie ograniczenia. Ale w rzeczywistości jest to całkiem łatwe.

Usunięcie widoku z hierarchii widoku powoduje usunięcie wszystkich ograniczeń z widoku i innych widoków poza nim. Więc po prostu usunąć widok od jej SuperView a następnie dodać go z powrotem:

// Remove constraints betwixt someView and non-descendants of someView. 
UIView *superview = someView.superview; 
[someView removeFromSuperview]; 
[superview addSubview:someView]; 

Jeśli istnieją jakieś ograniczenia Betwixt i jego potomkowie, ograniczenia te mogą być utrzymywane przez samą (ale nie może być w posiadaniu jakichkolwiek potomków ). Jeśli chcesz usunąć te ograniczenia, możesz to zrobić bezpośrednio:

// Remove any remaining constraints betwixt someView and its descendants. 
[someView removeConstraints:[someView constraints]]; 
+0

Dzięki, udało się! –

+0

Zauważyłem, że usunięcie podskoku z podglądu nie działa w przypadku ograniczeń zdefiniowanych dla tego podglądu (takich jak wysokość i szerokość przypiętej). –

+0

Druga część mojej odpowiedzi usunie ograniczenia szerokości i wysokości. –