How to changing barTintColor without animation on iOS7?

1.3k views Asked by At

On iOS7, we can changing navigation bar's color by

self.navigationController.navigationBar.barTintColor = [UIColor redColor];

But this method has a fade animation when changing the barTintColor, So does anynone know how to prevent this animation and change the color immediately?

To be more specific, I wrote a test program whose window's root controller is navigationController. And in the navigationController, there is a view controller with 3 buttons. 3 buttons all bind to the following action:

- (void)onClick:(id)sender
{
    UIColor *color = nil;
    if (sender == self.redButton)
    {
        color = [UIColor redColor];
    }
    else if (sender == self.blueButton)
    {
        color = [UIColor blueColor];
    }
    else if (sender == self.blackButton)
    {
        color = [UIColor blackColor];
    }

    self.navigationController.navigationBar.barTintColor = color

//    [UIView animateWithDuration:0 animations:^{
//        self.navigationController.navigationBar.barTintColor = color;
//    }];

//    [CATransaction begin];
//    [CATransaction setDisableActions:YES];
//    self.navigationController.navigationBar.barTintColor = color;
//    [CATransaction commit];

}

Both changing the animation duration to 0 or using [CATransaction setDisableActions:YES] doesn't work, the animation still exists.

Hope someone can help, thank you!

4

There are 4 answers

1
zbMax On

Try with

[UIView animateWithDuration:0 animations:^{
        self.navigationController.navigationBar.barTintColor = [UIColor redColor];
}];
2
tarmes On

You need to disable the implicit animation. You can do this as follows:

[CATransaction begin];
[CATransaction setDisableActions: YES];

self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:125.0/255.0 green:90.0/255.0 blue:146.0/255.0 alpha:1];

[CATransaction commit];

This technique works for any implicit animation. An implicit animation is an animation that's created for you by iOS when you change an animatable property. See here for more information:

https://developer.apple.com/library/ios/documentation/cocoa/conceptual/coreanimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html

1
Musket On

Try setting barTintColor to nil before setting it again.

self.navigationController.navigationBar.barTintColor = nil;
self.navigationController.navigationBar.barTintColor = color;

I had a similar problem, and it fixed it for me. Hope it helps.

1
jaggedcow On

Set translucent property on the UINavigationBar to NO, then reset it after the bar tint is changed

self.navigationBar.translucent = NO;
self.navigationBar.barTintColor = [UIColor magentaColor];
self.navigationBar.translucent = YES;