Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraints based layout not animated when the status bar height changes on iOS

I have this view that used to have autoresizingMask = UIViewAutoresizingFlexibleHeight

When the status bar would animate its height (like when hanging up a phone call), the view's height would animate and increase.

But with auto layout I'm replacing this autoresizingMask with constraints:

UIView *orangeView = [[UIView alloc] initWithFrame:CGRectZero];
orangeView.translatesAutoresizingMaskIntoConstraints = NO;
orangeView.backgroundColor = [UIColor orangeColor];

[self.view addSubview:orangeView];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[orangeView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(orangeView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(40)-[orangeView]-(190)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(orangeView)]];

But now, the change in my layout is not animated with the status bar, it's just changed without any animations.

Now I know that I should call -layoutIfNeeded in an animation block when using constraints-based layout. But here I'm not the one creating the animation block! So is there a way to animate the change?

Does it mean I have to found a place in my code that would be executed during this animation block I didn't initiate? I tried to set [self.view layoutIfNeeded] in my controller when the UIApplicationWillChangeStatusBarFrameNotificationis fired, but it doesn't work.

like image 396
Baldoph Avatar asked Mar 20 '13 11:03

Baldoph


1 Answers

Make sure you add your constraints in the updateConstraints method.

Here's what the docs say:

Custom views that set up constraints themselves should do so by overriding this method. When your custom view notes that a change has been made to the view that invalidates one of its constraints, it should immediately remove that constraint, and then call setNeedsUpdateConstraints to note that constraints need to be updated.

like image 192
Christoph Wimberger Avatar answered Oct 15 '22 12:10

Christoph Wimberger