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];
}
Dzięki, udało się! –
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). –
Druga część mojej odpowiedzi usunie ograniczenia szerokości i wysokości. –