0
votes

I have a view controller, say, Rootvc. It has a button which when clicked has to make another view controller, say vc1 to appear like a drawer. Rootvc has its navigation bar hidden but vc1 has to show its navigation bar. At first I was using CATransition to push vc1. But it turned Rootvc black. So, I went for custom transition. My problem is that when I tap the button to push vc1, its navigation bar appears instantly and then vc1 follows with animation. Same is the case on popping vc1. Navigation bar of vc1 disappears first and then view of vc1 slides to left. Is there any way out to make navigation bar animate along with its view controller. I am hiding navigation bar of Rootvc in viewWillAppear

self.navigationController.navigationBarHidden = YES; 

and again in vc1 in viewWillAppear I set

self.navigationController.navigationBarHidden = NO;

On tapping button on Rootvc

- (IBAction)btnMenuTapped:(UIButton *)sender {
        [self.navigationController pushViewController:vc1Obj animated:YES];
}

On tapping back button on vc1

-(void)backBtnTapped:(UIButton*)sender{
        [self.navigationController popViewControllerAnimated:YES];
}

Code for custom transition is

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
        UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

        CGRect fromVCFrame = fromVC.view.frame;
        CGFloat width = toVC.view.frame.size.width;
//to push vc1     
        if ([fromVC isKindOfClass:[Rootvc class]]) {
            [[transitionContext containerView] addSubview:toVC.view];
            toVC.view.frame = CGRectOffset(fromVCFrame, -width, 0);

            [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
                fromVC.view.frame = CGRectOffset(fromVCFrame, width, 0);
                toVC.view.frame = fromVCFrame;
            } completion:^(BOOL finished) {
                fromVC.view.frame = fromVCFrame;
                [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            }];
        }
//to pop vc1
       if ([fromVC isKindOfClass:[vc1 class]]) {
        [[transitionContext containerView] addSubview:toVC.view];
        [[transitionContext containerView] sendSubviewToBack:toVC.view];
        toVC.view.frame = fromVCFrame;

        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            fromVC.view.frame = CGRectOffset(fromVCFrame, -width, 0);
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }

    }

also I have to ask if I have to perform something like removeFromSuperview on popping vc1

1

1 Answers

0
votes

Try setting using the animated version of setNavigationBarHidden:

[self.navigationController setNavigationBarHidden: YES animated: YES];