iOS 7 : Disable UINavigationBar Translucency For Entire App
Solution 1
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
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
It seems very simple with this code in appDelegate
didFinishLaunchingWithOptions
(works fine with iOS 8 and above versions)
[[UINavigationBar appearance] setTranslucent:NO];
Solution 4
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 5
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.
Comments
-
MikeS almost 2 years
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 inTerminating 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...
-
MikeS over 10 yearsThat 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.
-
huggie over 10 yearsHow about using method swizzle and just patch it on viewDidLoad?
-
Sam Grossberg over 10 yearsYou can also subclass UINavigationController and override the designated initializer to set self.navigationBar.translucent = NO
-
Christian Schnorr over 10 yearsDoing this is -drawRect:... Have fun watching your app break ;)
-
Sjoerd Perfors about 10 yearswhen applying this to the uinavigationbar you will end up with a black statusbar :(
-
Roshan about 10 yearsIf you are using the storyboard, deselect transparency from UINavigationController.
-
Andy Riordan over 9 yearsNote 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/… -
Daniel Rinser over 9 yearsIt 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.
-
kshitij godara over 9 yearsso you have to set each and every stack by like this
-
Daniel Rinser over 9 yearsYes, 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. -
atreat about 9 yearsThis solution is not complete if you display any modal view controllers or anything that does not occur within the navigation controller.
-
atreat about 9 yearsIts 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.
-
Septronic over 8 yearsI'm not sure why someone gave this a negative score, I used this and worked perfectly! I've voted it up!
-
Septronic over 8 yearsDefinitely maybe! Thanks anyway.