iOS 7 UINavigationController NavBar每个控制器颜色动画

Luk*_*cka 6 cocoa-touch uinavigationbar uinavigationcontroller ios7

有没有什么办法有不同barTintColorUINavigationControllerUINavigationBar不同的推控制器与平滑的色彩过渡动画

我希望UINavigationBarUINavigationController推/动画动画期间有一个平滑的色彩动画,理想情况下也是交互式弹出(基于手势的控制器弹出).

我为什么需要这个?我想让导航堆栈中的1个控制器具有不同的色调颜色,指示某个任务的状态(红色/绿色等).

到目前为止我尝试了什么:

  • viewWillAppear(查看生命周期)方法,但没有办法动画barTintColor(像setBarTintColor:animated:)
  • 为了改变barTintColor[UIView animation...]块中,但只是古怪动画(可能)的一些背景层,而不是平滑的颜色过渡的帧.
  • 要改变barTintColor[UIView transitionWithView:...]与块UIViewAnimationOptionTransitionCrossDissolve,但这并不改变动画.在动画持续时间后立即更改为新的色调颜色
  • 我有一个想法是实现新的iOS 7自定义过渡计算和在进行过程中改变导航栏的颜色,但这似乎是一个大的矫枉过正(特别是如果我想保持原始动画外观到处)

感谢大家的任何想法和答案

小智 26

你可以通过使用来获得这个UIViewControllerTransitionCoordinator.

  1. 将示例代码复制到AController并自定义颜色.
  2. 将示例代码复制到BController并自定义颜色.
  3. 而已!在UINavigationController推动/弹出过渡期间,它AController的风格将顺畅淡入/淡出BController风格.

示例代码:

-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [[self transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

        // text color
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

        // navigation items and bar button items color
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

        // background color
        self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
    } completion:nil];
}
Run Code Online (Sandbox Code Playgroud)