Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView transitionFromView:toView: and layout constraints

What is the correct way to use [UIView transitionFromView:toView:...] with layout constraints?

I want to install constraints in the superview constraining toView. I can't do it before the transition call as toView doesn't yet have a superview. (Same for after the call but before the run loop progresses.) Waiting until the completion block to install it means the view will have animated without constraints.

like image 787
PlayfulGeek Avatar asked Dec 14 '12 22:12

PlayfulGeek


1 Answers

I'd use the UIViewAnimationOptionShowHideTransitionViews option, which allows both the toView and fromView to be in the view hierarchy before the transition, but shows one and hides the other.

Set toView to be hidden, add it to the superview, and install the constraints before the transition. You can then remove the old view in the completion block. Something like this:

[toView setHidden: YES];
[containerView addSubview: toView];
[containerView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"|[toView]|" options: 0 metrics: nil views: NSDictionaryOfVariableBindings(containerView, toView)]];
[containerView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|[toView]|" options: 0 metrics: nil views: NSDictionaryOfVariableBindings(containerView, toView)]];

[UIView transitionFromView: fromView toView: toView duration: 1.0 options: UIViewAnimationOptionTransitionFlipFromBottom | UIViewAnimationOptionShowHideTransitionViews completion:^(BOOL finished) {
    [fromView removeFromSuperview];
}];
like image 77
Peter E Avatar answered Oct 05 '22 10:10

Peter E