55

Is there a way to disable UINavigationBar Translucency for an entire application?

I'm aware that using [self.navigationController.navigationBar setTranslucent:NO] can fix this issue for a single controller, but I have a lot of UINavigationBars in my application and this is a pretty tedious solution.

I've tried [[UINavigationBar appearance] setTranslucent:NO], but that functionality is surprisingly not supported. Doing that results in Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** Illegal property type, c for appearance setter, _installAppearanceSwizzlesForSetter:'

If I HAVE to, I can go through my entire app setting UINavigationBars to disable translucency one by one, but there must be some more elegant solution to this issue...

3
  • Are you using storyboards?
    – BlueBear
    Commented Sep 18, 2013 at 22:22
  • Unfortunately, I am not.
    – MikeS
    Commented Sep 18, 2013 at 22:24
  • 4
    Better yet, is there a way to force my views to layout in a sane manner without needing to turn the translucency off? This is ridiculous
    – powerj1984
    Commented Sep 26, 2013 at 20:34

9 Answers 9

33

if you set the translucence of the first navigation bar in the stack to false [self.navigationController.navigationBar setTranslucent:NO], it will reflect in all the following NavigationViewController that are pushed to that stack.

2
  • 2
    If you are using the storyboard, deselect transparency from UINavigationController.
    – Roshan
    Commented Apr 29, 2014 at 19:08
  • 2
    This solution is not complete if you display any modal view controllers or anything that does not occur within the navigation controller.
    – atreat
    Commented Apr 28, 2015 at 14:58
19

Here is a Swift solution if you want to apply this Styling to the whole app.

in the AppDelegate class add this to the didFinishLaunchingWithOptions:

For Swift 2:

UINavigationBar.appearance().translucent = false

For Swift 3+:

UINavigationBar.appearance().isTranslucent = false
8

It seems very simple with this code in appDelegate didFinishLaunchingWithOptions (works fine with iOS 8 and above versions)

[[UINavigationBar appearance] setTranslucent:NO];
1
  • I'm not sure why someone gave this a negative score, I used this and worked perfectly! I've voted it up!
    – Septronic
    Commented Dec 23, 2015 at 4:05
3

I think you are right about no appearance proxy being available for this property. Are you using UINavigationControllers or UINavigationBar objects? If you are using UINavigationBars you could subclass it and create a non-translucent nav bar.

Header file:

#import <UIKit/UIKit.h>

@interface ABCNonTranslucentNavBar : UINavigationBar

@end

Implementation file:

#import "ABCNonTranslucentNavBar.h"

@implementation ABCNonTranslucentNavBar

- (void)drawRect:(CGRect)rect
{
  [self setTranslucent:NO];
}

Then just replace the UINavigationBars with your subclass. You could also do something similar with a subclassed UINavigationController.

4
  • 1
    That is definitely an interesting idea, but a bit of a pain. Maybe there is an easier way...? Regardless of the answer to this question as of right now, I would be shocked if iOS 7.1 doesn't add a translucency appearance proxy. Also, to answer your question, I am using UINavigationControllers, not just the bar.
    – MikeS
    Commented Sep 19, 2013 at 0:19
  • How about using method swizzle and just patch it on viewDidLoad?
    – huggie
    Commented Oct 1, 2013 at 15:37
  • You can also subclass UINavigationController and override the designated initializer to set self.navigationBar.translucent = NO Commented Oct 23, 2013 at 0:09
  • 7
    Doing this is -drawRect:... Have fun watching your app break ;) Commented Jan 31, 2014 at 13:47
3

Adding this in case anyones still battling this.

You can fool it though by specifying a non exist image, which will make the nav bar INCLUDING it's tool bar go opaque

[[UIToolbar appearance] setBackgroundColor:[UIColor colorWithRed:219.0/255.0 green:67.0/255.0 blue:67.0/255.0 alpha:1.0]];

[[UIToolbar appearance] setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
1
  • when applying this to the uinavigationbar you will end up with a black statusbar :( Commented Mar 19, 2014 at 14:36
2

I know this is old, but this might come in handy for someone;

You can use a category, and within it* set the property [translucent][1]

@implementation UINavigationBar (MakeTranslucent)

-(void)willMoveToWindow:(UIWindow *)newWindow {
    [super willMoveToWindow:newWindow];


    self.translucent = NO;
}
@end
  • I used willMoveToWindow, I do not know whether this is a good idea so UAYOR.
2
  • 1
    Note that this is dangerous, as it will not call the original UINavigationBar implementation of willMoveToWindow:. The category method replaces the original implementation of the method entirely, so you're actually skipping the original UINavigationBar implementation and calling UIView's implementation directly. stackoverflow.com/questions/6168058/… Commented Dec 19, 2014 at 20:46
  • Its a terrible idea to override methods using categories. In fact, in iOS 8.3 it seems the category methods which override class methods will not be called.
    – atreat
    Commented Apr 28, 2015 at 14:59
1

See the excerpt from UIKit code documentation:

/*
     New behavior on iOS 7.
     Default is YES.
     You may force an opaque background by setting the property to NO.
     If the navigation bar has a custom background image, the default is inferred 
     from the alpha values of the image—YES if it has any pixel with alpha < 1.0
     If you send setTranslucent:YES to a bar with an opaque custom background image
     it will apply a system opacity less than 1.0 to the image.
     If you send setTranslucent:NO to a bar with a translucent custom background image
     it will provide an opaque background for the image using the bar's barTintColor if defined, or black
     for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
     */

Correct Swift 4 solution is

UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().backgroundColor = .white
0

I think appearance api does not support translucent property of navigation bar . But you can do this for whole App like this , please have a look at this code --

here Menu Screen is a root view controller .

MenuScreen *ms = [[MenuScreen alloc]initWithNibName:@"MenuScreen" bundle:nil];

UINavigationController *nv = [[UINavigationController alloc]initWithRootViewController:ms];

//This will set property for whole App.
[nv.navigationBar setTranslucent:NO];

self.window.rootViewController = nv ;
3
  • It will only set it to translucent for this one navigation stack. If you have any modal navigation controllers, multiple tabs or any other kind of more complex navigation patterns, it won't work. Commented Jan 10, 2015 at 13:11
  • so you have to set each and every stack by like this Commented Jan 12, 2015 at 6:14
  • Yes, sure. But the point of this question was if there is a way to set it globally (like via UIAppearance), and that's exactly not what you're doing. Moreover, you're saying in your answer that it "will be set for the whole app", which is not true. Commented Jan 12, 2015 at 15:48
-3

If you don't use storyboard, but IB, set the navigation bar style of your MainWindows.xib to NOT translucent and set as color not the clear color.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.