【问题标题】:On pushing a view controller through custom transition navigation bar does not animate通过自定义转换导航栏推送视图控制器时不会动画
【发布时间】:2016-05-30 10:35:03
【问题描述】:

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

self.navigationController.navigationBarHidden = YES; 

我又在 vc1 的 viewWillAppear 中设置了

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 之类的操作

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    尝试使用setNavigationBarHidden的动画版设置:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      • 2018-09-24
      • 2019-03-11
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多