문제

I have a custom UIView that lays out its "subviews" using auto layout with some vertical spacing constraints.

@"V:|-(10)-[_label]-(10)-|

Now this view is added to a controller's view where I want to have the possibility to collapse/expand it. To do this, I added a height constraint that I adjust either to 0 or to a given height.

Code:

- (void)toggleView:(id)sender
{
    self.viewVisible = !self.viewVisible;
    self.headerHeightConstraint.constant = self.viewVisible ? 100 : 0;
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    }];
}

The problem is that when I collapse the view, I have some auto layout exceptions because the 0 height of the view conflicts with the vertical spacing of the inner label view.

So the question is, how can I collapse a view that has some inner height constraints ?

"Here" is a sample project.

cheers, Jan

도움이 되었습니까?

해결책

Just change the priority of the vertical spacing. UILayoutConstraints have a default priority of 1000 when you create them, so you can just change the priority of the vertical spacing to less than that so the height constraint will always be prioritized over the vertical spacing.

Change the visual format to the ff:

@"V:|-(10@999)-[_label]-(10@999)-|
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top