iOS 7 : Disable UINavigationBar Translucency For Entire App

你。 提交于 2019-11-28 21:00:47
Roshan

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.

dan

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

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

[[UINavigationBar appearance] setTranslucent:NO];

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.

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

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.

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 ;

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!