iOS 7 UINavigationController NavBar 每个控制器颜色动画 [英] iOS 7 UINavigationController NavBar per controller color animation

查看:18
本文介绍了iOS 7 UINavigationController NavBar 每个控制器颜色动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法可以让UINavigationControllerUINavigationBar在不同的推送控制器上有不同的barTintColor具有平滑的颜色过渡动画?

Is there any way to have different barTintColor of UINavigationController's UINavigationBar on different pushed controllers with smooth color transition animation?

我希望在 UINavigationController 的推送/弹出动画期间拥有 UINavigationBar 的色调颜色的平滑动画并且最好也交互式弹出(基于手势的控制器弹出).

I'd like to have a smooth animation of UINavigationBar's tint color during UINavigationController's push/pop animation and ideally also interactive pop (gesture based controller pop).

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

Why do I need this? I'd like to have 1 controller in the navigation stack to have different tint color indicating status of some task (red / green etc.).

到目前为止我尝试过的:

What I have tried so far:

  • viewWillAppear(视图生命周期)方法,但是没有办法为 barTintColor 设置动画(比如 setBarTintColor:animated:)
  • 要更改 [UIView animation...] 块中的 barTintColor,但这只是奇怪地为(可能)某些背景层的帧设置动画,而不是平滑的颜色过渡.
  • 使用 UIViewAnimationOptionTransitionCrossDissolve 更改 [UIView transitionWithView:...] 块中的 barTintColor,但这不会产生动画变化.在动画持续时间后立即更改为新的色调
  • 我有一个想法,即在执行过程中实现新的 iOS 7 自定义过渡计算和更改导航栏的颜色,但这似乎有点矫枉过正(特别是如果我想在任何地方都保持原始动画外观)
  • viewWillAppear (view lifecycle) methods, but there is no way to animate the barTintColor (like setBarTintColor:animated:)
  • To change barTintColor in [UIView animation...] block, but that just weirdly animates frame of (probably) some background layer instead of smooth color transition.
  • To change barTintColor in [UIView transitionWithView:...] block with UIViewAnimationOptionTransitionCrossDissolve, but that does not animate change. Just instantly changes to new tint color after the animation duration
  • I had an idea of implementing new iOS 7 custom transition calculating and changing color of navbar during progress, but that seems to be big overkill (specially if I want to keep original animation appearance everywhere)

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

Thank you everyone for any ideas and answers

推荐答案

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

You can get this by using UIViewControllerTransitionCoordinator.

  1. 将示例代码复制到 AController 并自定义颜色.
  2. 将示例代码复制到 BController 并自定义颜色.
  3. 就是这样!在 UINavigationController 的 push/pop 转换期间,AController 的样式会平滑地淡入/淡出 BController 的样式.
  1. Copy the example code to the AController and customize the colors.
  2. Copy the example code to the BController and customize the colors.
  3. That's it! During UINavigationController's push/pop transition, the AController's style will smoothly fade in/out to BController's style.

示例代码:

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

这篇关于iOS 7 UINavigationController NavBar 每个控制器颜色动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆