Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS7 Custom ViewController transition and Top Layout Guide

I implemented a custom UIViewController Transition in my App, which replaces the navigation controllers built in push animation.

Everything works so far, except the toplayoutguide in the newly pushed view controller is 0 although the new view controller inherited the navigation bar from the old view controller. It should be 64.0 (Statusbar height + Navigation bar height), where it is 0.0 now. So all objects, which are attached to the top layout guide in the storyboard now appear 64 points too high (below the translucent bar).

When I disable the custom View Transition the top layout guide will have the expected value. I tried to call layoutSubviews and updateConstraints "all over the place". In the view controller as well as in the navigationcontroller.

As I understand the navigationcontroller (parentviewcontroller) should update the toplayoutguide of the new view controller, but apparently I am missing something in my custom transitioning code, which triggers the update to the correct value for the toplayoutguide.

Here's my custom transition code which is an object set as delegate of the navigationcontroller:

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.7;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIView *animationContainerView = [transitionContext containerView];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *toView = [toVC view];
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *fromView = [fromVC view];

    CGRect endFrame = [transitionContext finalFrameForViewController:toVC];
    CGRect startFrame;

    startFrame = CGRectOffset(endFrame, 0, endFrame.size.height);

    if (self.operation == UINavigationControllerOperationPop) {
        [animationContainerView insertSubview:toView belowSubview:fromView];
        [toView setFrame:endFrame];
    }
    else{
        [toView setFrame:startFrame];
        [animationContainerView insertSubview:toView aboveSubview:fromView];
    }

    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 usingSpringWithDamping:0.7 initialSpringVelocity:0.8 options:UIViewAnimationOptionBeginFromCurrentState animations:^{

        if (self.operation == UINavigationControllerOperationPop) {
            [fromView setFrame:startFrame];
            [fromView layoutIfNeeded];
        }
        else{
            [toView setFrame:endFrame];
            [toView layoutIfNeeded];
        }

    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

Nothing really fancy happens there. Just the view sliding from the bottom up with some built in dynamics.

The problem is, that the objects attached to the top layout guide now are under the navigation bar, as the top layout guide length == 0.

I can't figure out what I need to do, so that the view controller's toplayoutguide is set to the correct value.

The push navigation is performed "plain vanilla" with a push storyboard segue. All I do, before calling performSegueWithIdentifier is to set the navigationcontrollers delegate.

Here's the code:

    self.navigationController.delegate = [[My_CustomNavigationTransitionDelegate alloc] init];
    [self performSegueWithIdentifier:@"infosSegue" sender:nil];

What do I miss?

like image 400
alex da franca Avatar asked Nov 05 '13 12:11

alex da franca


1 Answers

I was having an issue where the bottomLayoutGuide property would set itself to zero length and then would cause my buttons above the tab bar to fall below to tab bar with the autolayout.

Have you looked at doing this

[self.navigationController.view setNeedsLayout]

I put it into my viewwillappear and I stopped getting a zero length on the bottomLayoutGuide property. Maybe that would help you out with your topLayoutGuide property too.

like image 54
EBM Avatar answered Oct 14 '22 12:10

EBM