Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Auto Layout with container transitions?

How can you use Auto Layout with the UIViewController container transition method:

-(void)transitionFromViewController:(UIViewController *)fromViewController                    toViewController:(UIViewController *)toViewController                             duration:(NSTimeInterval)duration                             options:(UIViewAnimationOptions)options                          animations:(void (^)(void))animations                          completion:(void (^)(BOOL finished))completion; 

Traditionally, using Springs/Struts, you set the initial frames (just before calling this method) and set up the final frames in the animation block you pass to the method.

That method does the work of adding the view to the view hierarchy and running the animations for you.

The problem is that you we can't add initial constraints in the same spot (before the method call) because the view has not yet been added to the view hierarchy.

Any ideas how I can use this method along with Auto Layout?

Below is an example (Thank you cocoanetics) of doing this using Springs/Struts (frames) http://www.cocoanetics.com/2012/04/containing-viewcontrollers

- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController {      // XXX We can't add constraints here because the view is not yet in the view hierarchy     // animation setup      toViewController.view.frame = _containerView.bounds;     toViewController.view.autoresizingMask = _containerView.autoresizingMask;      // notify     [fromViewController willMoveToParentViewController:nil];     [self addChildViewController:toViewController];      // transition     [self transitionFromViewController:fromViewController                       toViewController:toViewController                               duration:1.0                                options:UIViewAnimationOptionTransitionCurlDown                             animations:^{                             }                             completion:^(BOOL finished) {                                 [toViewController didMoveToParentViewController:self];                                 [fromViewController removeFromParentViewController];                             }]; } 
like image 893
Mike Vosseller Avatar asked Jan 02 '14 19:01

Mike Vosseller


People also ask

When should you use Auto Layout in Figma?

Auto layout is a property you can add to frames and components. It lets you create designs that grow to fill or shrink to fit, and reflow as their contents change. This is great when you need to add new layers, accommodate longer text strings, or maintain alignment as your designs evolve.

How do you override a Figma auto Layout?

You'll need to select the instance itself, and not a specific layer within it. In the right sidebar, click the next to the component name. Select Push overrides to Main from the options. Figma will apply your overrides to the main component.

How do you set a container in Figma?

Objects in an auto layout frame set to Fill container stretch to the width and/or height of their parent frame. Tip! Hold ⌥ Option or Alt and double-click the vertical or horizontal edge of an auto layout object's bounding box to set it to Fill container.


1 Answers

Starting to think the utility method transitionFromViewController:toViewController:duration:options:animations:completion can not be made to work cleanly with Auto Layout.

For now I've replaced my use of this method with calls to each of the "lower level" containment methods directly. It is a bit more code but seems to give greater control.

It looks like this:

- (void) performTransitionFromViewController:(UIViewController*)fromVc toViewController:(UIViewController*)toVc {      [fromVc willMoveToParentViewController:nil];     [self addChildViewController:toVc];      UIView *toView = toVc.view;     UIView *fromView = fromVc.view;      [self.containerView addSubview:toView];      // TODO: set initial layout constraints here      [self.containerView layoutIfNeeded];      [UIView animateWithDuration:.25                           delay:0                         options:0                      animations:^{                           // TODO: set final layout constraints here                           [self.containerView layoutIfNeeded];                       } completion:^(BOOL finished) {                          [toVc didMoveToParentViewController:self];                          [fromView removeFromSuperview];                          [fromVc removeFromParentViewController];                      }]; } 
like image 127
Mike Vosseller Avatar answered Sep 23 '22 16:09

Mike Vosseller