31
votes

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 enter image description here
  • When going from VC2 --> VC1 the nav bar instantly changes to the application default colour before the segue has time to complete. enter image description here
  • 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. enter image description here
  • When going from VC3 --> VC2 the nav bar instantly turns black and remains this way until the segue is complete. enter image description here

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.

11
Have you tried animating those changes with +[UIView animateWithDuration:animations:]?Palpatim
Yeah. It improves the transition but it's still going from opaque to transparent so towards the end of the segue you can see the black behind the navigation bar. The best workaround I've found so far is to set the UINavigationBar backgroundColor to the app default right before doing [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 suggestionalexgophermix
Not sure if this is till an issue for you, but the key item which seems to cause the unwanted effects is the change of the translucent property. Unlike the rest of the transition, the translucence seems to be applied immediately. A suggestion is to set the translucent property separately in viewDidAppear which delays the sudden change you see when push/pop starts. Especially where viewWillAppear would change this from NO to YES, delay that property change until viewDidAppear. Not perfect as full transparency is delayed until you set YES, but should get rid of the black bar effect.Rory McKinnel
That's roughly what I was doing before I posted here. I was hoping that there had been some pattern or workaround that I had missed but a combination of modifying background appearance and translucency in viewWillAppear and viewDidAppear 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
The best solution for this is here : stackoverflow.com/a/2406167/3411787Mohammad Zaid Pathan

11 Answers

18
votes

I finally found a decent solution!

There doesn't appear to be a proper way to smoothly transition from an opaque to transparent UINavigationBar BUT you can transition smoothly from a view controller with a visible status bar to one that has a hidden status bar.

This opens up a possible workaround which is to add the following in the viewWillAppear of VC2 from above:

[self.navigationController setNavigationBarHidden:YES animated:YES];

Once you have that, manually add a UINavigationBar to your xib and configure it to be transparent (and add all necessary UIBarButtonItem and views).

If everything is hooked up properly transitioning from VC1 to VC2 will hide the UINavigationBar at the same speed as the view transition and VC2 will show up with it's embedded UINavigationBar

Note: To make this work properly you'll have to make sure that in the viewWillAppear of View Controllers that can be accessed from VC2 you reset the UINavigationBar to be visible (if necessary) via:

[self.navigationController setNavigationBarHidden:NO animated:YES];

TL;DR - manually add a UINavigationBar to your transparent nav bar view controller and in its viewWillAppear hide the default one via setNavigationBarHidden:animated:

18
votes

The black color you're seeing is the background color of the UINavigationController's view. One way to minimize seeing it is to manipulate the background color of that view to the color of the outgoing / incoming view controller's view. This works well if you're working with solid colors. Another approach is to extend your views behind the opaque navigation bar using UIViewController.extendedLayoutIncludesOpaqueBars = YES;

4
votes

Set the UIWindow background color to your Navigation bar's tintColor.

3
votes

always use Translucent, plus add an uiview with a color below it and animate it's alpha?

1
votes

I've struggled with a nearly identical problem. There really aren't any smooth transitions using either a nav bar or toolbar as a blur. The best option that I've found* is to make an image out of the view and then use that image for the transition. Especially if you just need it for transitions, it's just about the cheapest option that still provides a great UI/UX.

*The one caveat is that some of the UI effects in a nav bar and toolbar don't show up when you take a snapshot, screenshot, or rasterize a UIView as an image. This is negligible if used for a transition.

1
votes

You can create your own navigation bar using just a UIView and that way you have complete control over its appearance, layout, fading etc. I gave up using UINavigationBar a while ago as it can be a pain to work with, as you are discovering, and now I never ever use it.

I have a root view controller which has a UIView which represents the navigation bar. If I want to do something like add a back button, change the color, show the navigation bar or hide it, change the transparency, etc. that is all controlled by the RVC and other view controllers call methods on the RVC to change the navigation bar depending upon their appearance requirements.

The RVC has a container view which is the full size of the controller view and the other view controllers get loaded into that. A little bit of configuration to get everything set up, but once done its a structure that can be used in every project that uses a navigation bar very quickly.

1
votes

So I struggled with this too.

Unfortunately I didn't have any success adding my own navigation bar via the storyboard. Instead, (and I warn you this is hacky) I add a view in the ViewController with the opaque navigation bar that has a negative margin and extends under the navigation bar.

When the ViewController with the transparent navigation bar is pushed the bar then immediately becomes transparent but due to the identically coloured view I have place directly behind it the change isn't noticeable. Et voila.

The code I have is pretty basic and written in C# (Xamarin) but for reasons of completeness....

  var backing = new UIView(new CGRect(0, -68, this.View.Frame.Width, 64f));
  backing.BackgroundColor = UIColor.Green;
  this.View.AddSubview(backing);
0
votes

Change background color work for me

window?.backgroundColor = Color.red.toUIColor()
0
votes

I have one more solution..for the ones who are now refactoring the project and cant add navigation bar everywhere.. This is not a perfect solution just like @alexgophermix one. But its not bad either:

let transition = CATransition()
        transition.duration = 0.6
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        transition.type = kCATransitionFade
        navigationController?.navigationBar.shadowImage = UIImage()
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.isTranslucent = true
        navigationController?.navigationBar.layer.add(transition, forKey: nil)
        navigationController?.view.backgroundColor = .clear

I am setting my nav bar in viewDidLoad() so this works everywhere and gives a slight different transition. In my case I was setting clear backgrounds somewhere and rest a gradient image. So this small change goes well for everywhere. This transition is not much noticeable but neither looks bad :)

0
votes

In my case, the black I was seeing was the navbar's background image. Instead of setting it to nil, I set it to match the background color of the view behind the transparent navbar inside viewWillAppear:

let image = UIImage(color: .white)
navigationController?.navigationBar.setBackgroundImage(image, for: .default)

Then, inside willMove and viewWillDisappear I reverted it back to the original color.

-1
votes

YPNavigationBarTransition is a configureable framework animating navigation bars.

YPNavigationBarTransition uses two fake navigationbars (UIToolBar indeed) to simulate bar transitions while making the real navigationbar transparent.

  1. Subclass your own UINavigationController and embed transitioncenter in it like YPNavigationController

  2. Implement YPNavigationBarProtocol for your content controllers respectively.

  3. Done.

A complete demo is include in the repo, checkout it for more details.