可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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...
回答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.
回答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
回答3:
It seems very simple with this code in appDelegate
didFinishLaunchingWithOptions
(works fine with iOS 8 and above versions)
[[UINavigationBar appearance] setTranslucent:NO];
回答4:
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.
回答5:
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];
回答6:
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.
回答7:
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 ;
回答8:
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
回答9:
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.