I'm running into problems reconfiguring the UINavigationBar
on iOS 7 and 8 when transitioning between views.
My application currently contains the following UIViewController
flow:
VC1 --> VC2 --> VC3
In this flow
- VC1 is the home screen and has an opaque
UINavigationBar
- VC2 has a translucent
UINavigationBar
- VC3 goes back to having an opaque
UINavigationBar
The problem I've been running into is that the transitions between these views are all very sloppy looking. To start with I tried the following:
in VC2
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// configure appearance
[self.navigationController.navigationBar configureTranslucentAppearance];
}
And in VC1 and VC3
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// configure appearance
[self.navigationController.navigationBar restoreDefaultAppearance];
}
Here are the implementations of the two helper functions listed above:
- (void)restoreDefaultAppearance {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[self setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor JTTextNavBar]}];
[self setTintColor:[UIColor JTTextNavBar]];
[self setBarTintColor:[UIColor JTBackgroundNavBarWithAlpha:1.0]];
[self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self setBackgroundColor:[UIColor JTBackgroundNavBarWithAlpha:1.0]];
[self setShadowImage:[UIImage navigationBarShadowImage]];
[self setTranslucent:NO];
}
- (void)configureTranslucentAppearance {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self setBackgroundColor:[UIColor clearColor]];
[self setShadowImage:[UIImage new]];
[self setTranslucent:YES];
}
This is the most basic way of handling this transition. It has the following visual artefacts:
- When going from VC1 --> VC2 the moment you begin the transition the navigation bar turns black. The animation completes normally
- When going from VC2 --> VC1 the nav bar instantly changes to the application default colour before the segue has time to complete.
- When going from VC2 --> VC3 the navigation bar instantly goes from translucent to the app nav bar color and then menu items and VC body animate in.
- When going from VC3 --> VC2 the nav bar instantly turns black and remains this way until the segue is complete.
None of these transitions look good at all. Ideally I would like the views to transition smoothly along with their new UINavigationBar
but the only way I've seen to do this successfully is to manually add a toolbar to each xib.
Any suggestions? Apologies if this description is confusing :(
Edit: Added cropped images of the UINavigationBar
and top portion of UIViewController
for each of the listed transitions.
+[UIView animateWithDuration:animations:]
? – Palpatim[self.navigationController.navigationBar configureTranslucentAppearance];
This at least doesn't show a temporary black bar when going from opaque to translucent, though there's still the issue of translucent to opaque. Thanks for your suggestion – alexgophermixtranslucent
property. Unlike the rest of the transition, the translucence seems to be applied immediately. A suggestion is to set thetranslucent
property separately inviewDidAppear
which delays the sudden change you see when push/pop starts. Especially whereviewWillAppear
would change this from NO to YES, delay that property change untilviewDidAppear
. Not perfect as full transparency is delayed until you set YES, but should get rid of the black bar effect. – Rory McKinnelviewWillAppear
andviewDidAppear
both in VC1 and VC2 seems to be the only solution with out completely overriding the nav bar or its transition as suggested in the answers below. – alexgophermix