iOS 7 : Disable UINavigationBar Translucency For Entire App

IosUinavigationbarIos7

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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

Solution 3 - Ios

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

[[UINavigationBar appearance] setTranslucent:NO];

Solution 4 - Ios

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.

Solution 5 - Ios

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

Solution 6 - Ios

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.

Solution 7 - Ios

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

Solution 8 - Ios

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 ;

Solution 9 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeSView Question on Stackoverflow
Solution 1 - IosRoshanView Answer on Stackoverflow
Solution 2 - IosdanView Answer on Stackoverflow
Solution 3 - IosSudhin DavisView Answer on Stackoverflow
Solution 4 - IosKyle CleggView Answer on Stackoverflow
Solution 5 - IosJulianBView Answer on Stackoverflow
Solution 6 - IosBDRView Answer on Stackoverflow
Solution 7 - IosfewlinesofcodeView Answer on Stackoverflow
Solution 8 - Ioskshitij godaraView Answer on Stackoverflow
Solution 9 - IosthomasView Answer on Stackoverflow