简体   繁体   English

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

[英]On pushing a view controller through custom transition navigation bar does not animate

I have a view controller, say, Rootvc . 我有一个视图控制器,例如Rootvc It has a button which when clicked has to make another view controller, say vc1 to appear like a drawer. 它具有一个按钮,单击该按钮时必须使另一个视图控制器(例如vc1)看起来像抽屉一样。 Rootvc has its navigation bar hidden but vc1 has to show its navigation bar. Rootvc隐藏了其导航栏,但是vc1必须显示其导航栏。 At first I was using CATransition to push vc1. 起初,我使用CATransition推送vc1 But it turned Rootvc black. 但是它使Rootvc变黑了。 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. 我的问题是,当我点击按钮以按vc1时,其导航栏立即出现,然后vc1跟随动画。 Same is the case on popping vc1. 弹出vc1时也是如此。 Navigation bar of vc1 disappears first and then view of vc1 slides to left. vc1的导航栏首先消失,然后vc1的视图向左滑动。 Is there any way out to make navigation bar animate along with its view controller. 有什么方法可以使导航栏及其视图控制器产生动画效果。 I am hiding navigation bar of Rootvc in viewWillAppear 我在viewWillAppear中隐藏了Rootvc的导航栏

self.navigationController.navigationBarHidden = YES; 

and again in vc1 in viewWillAppear I set 并再次在vc1中设置了viewWillAppear

self.navigationController.navigationBarHidden = NO;

On tapping button on Rootvc 在Rootvc上点击按钮

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

On tapping back button on vc1 在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 我也要问我是否必须在弹出vc1上执行诸如removeFromSuperview之类的操作

尝试使用setNavigationBarHidden的动画版本进行setNavigationBarHidden

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将视图推入选项卡栏控制器中的导航控制器时无法设置动画 - Pushing view onto navigation controller in tab bar controller does not animate 在通过导航控制器使用自定义动画过渡到SecondViewController后,它处于非活动状态 - Inactive view in SecondViewController after the transition to it with custom animation through the navigation controller 如何使用导航栏转换到视图控制器? - How to make the transition to a view controller with a navigation bar? 在iPad上的popover中推送导航视图控制器时动画popoverContentsize - Animate popoverContentsize when pushing navigation view controller in popover on iPad 从标签栏按钮将视图控制器推到导航控制器上 - Pushing a view controller onto a navigation controller from a tab bar button 推送视图控制器动画失败 - Pushing View Controller Fails to Animate 推送到View Controller时,导航栏覆盖了我的内容 - Navigation Bar covers my content when pushing to a View Controller 按下视图控制器时如何隐藏导航栏? - How can I hide the navigation bar when pushing a view controller? 按下视图控制器后,导航栏中出现阴影 - Shadow in navigation bar appears after pushing view controller 推送视图控制器后导航栏不会显示 - Navigation bar won't show after pushing a view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM