iOS 7 UINavigationController NavBar 每个控制器颜色动画 [英] iOS 7 UINavigationController NavBar per controller color animation
问题描述
有没有什么办法可以让UINavigationController
的UINavigationBar
在不同的推送控制器上有不同的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 thebarTintColor
(likesetBarTintColor: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 withUIViewAnimationOptionTransitionCrossDissolve
, 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
.
- 将示例代码复制到
AController
并自定义颜色. - 将示例代码复制到
BController
并自定义颜色. - 就是这样!在
UINavigationController
的 push/pop 转换期间,AController
的样式会平滑地淡入/淡出BController
的样式.
- Copy the example code to the
AController
and customize the colors. - Copy the example code to the
BController
and customize the colors. - That's it! During
UINavigationController
's push/pop transition, theAController
's style will smoothly fade in/out toBController
'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屋!