通过自定义转换导航栏推送视图控制器时不会设置动画

时间:2016-02-18 10:09:31

标签: ios objective-c

我有一个视图控制器,比方说, Rootvc 。它有一个按钮,当点击它时必须制作另一个视图控制器,比如说 vc1 就像抽屉一样。 Rootvc隐藏了导航栏,但vc1必须显示其导航栏。起初我正在使用 CATransition 推送vc1。但它将Rootvc变为黑色。所以,我去了自定义过渡。我的问题是当我点击按钮推vc1时,它的导航栏立即出现,然后vc1跟随动画。弹出vc1的情况也是如此。 vc1的导航栏首先消失,然后vc1的视图向左滑动。有没有办法让导航栏与其视图控制器一起动画。我在viewWillAppear中隐藏了Rootvc的导航栏

self.navigationController.navigationBarHidden = YES; 

并再次在viewWillAppear的vc1中设置

self.navigationController.navigationBarHidden = NO;

在Rootvc上点击按钮

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

点击vc1上的后退按钮

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

自定义转换代码

-(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]];
        }];
    }

    }

我还要问我是否必须在弹出vc1时执行类似 removeFromSuperview 的内容

1 个答案:

答案 0 :(得分:0)

尝试使用动态版setNavigationBarHidden进行设置:

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