Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

cocoa touch - iOS 7 UINavigationController NavBar per controller color animation

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

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).

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 (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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get this by using UIViewControllerTransitionCoordinator.

  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.

Example code:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...