iOS 7:禁用整个应用程序的 UINavigationBar 半透明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18883301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:20:59  来源:igfitidea点击:

iOS 7 : Disable UINavigationBar Translucency For Entire App

iosuinavigationbarios7

提问by MikeS

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

有没有办法为整个应用程序禁用 UINavigationBar 半透明?

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.

我知道使用[self.navigationController.navigationBar setTranslucent:NO]可以解决单个控制器的这个问题,但我的应用程序中有很多 UINavigationBars,这是一个非常乏味的解决方案。

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:'

我试过了[[UINavigationBar appearance] setTranslucent:NO],但令人惊讶的是不支持该功能。这样做会导致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...

如果必须的话,我可以通过我的整个应用程序设置 UINavigationBars 来一个一个地禁用半透明,但是必须有一些更优雅的解决方案来解决这个问题......

采纳答案by 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.

如果您将堆栈中第一个导航栏的半透明设置为false[self.navigationController.navigationBar setTranslucent:NO],它将反映在推入该堆栈的所有以下 NavigationViewController 中。

回答by dan

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

如果您想将此样式应用于整个应用程序,这是一个 Swift 解决方案。

in the AppDelegateclass add this to the didFinishLaunchingWithOptions:

AppDelegate班级中将此添加到didFinishLaunchingWithOptions

For Swift 2:

对于 Swift 2:

UINavigationBar.appearance().translucent = false

For Swift 3:

对于 Swift 3:

UINavigationBar.appearance().isTranslucent = false

回答by Sudhin Davis

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

这段代码看起来很简单appDelegatedidFinishLaunchingWithOptions(适用于 iOS 8 及更高版本)

[[UINavigationBar appearance] setTranslucent:NO];

回答by JulianB

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

回答by Kyle Clegg

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.

我认为您认为此属性没有可用的外观代理是正确的。你在使用 UINavigationControllers 还是 UINavigationBar 对象?如果您使用 UINavigationBars,您可以将其子类化并创建一个非半透明的导航栏。

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.

然后只需将 UINavigationBars 替换为您的子类。您还可以使用子类 UINavigationController 执行类似操作。

回答by BDR

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]

您可以使用一个类别,并在其中*设置属性 [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.
  • 我用的是willMoveToWindow,不知道UAYOR这样是否是个好主意。

回答by fewlinesofcode

See the excerpt from UIKit code documentation:

请参阅 UIKit 代码文档的摘录:

/*
     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

正确的 Swift 4 解决方案是

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

回答by kshitij godara

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

我认为外观 api 不支持导航栏的半透明属性。但是您可以像这样对整个应用程序执行此操作,请查看此代码-

here Menu Screen is a root view controller .

这里的 Menu Screen 是一个根视图控制器。

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 ;

回答by thomas

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.

如果您不使用故事板,而是使用 IB,请将 MainWindows.xib 的导航栏样式设置为不透明,并设置为颜色而不是清晰的颜色。