55

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

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

我试过[[UINavigationBar appearance] setTranslucent:NO]了,但令人惊讶的是不支持该功能。这样做会导致Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** Illegal property type, c for appearance setter, _installAppearanceSwizzlesForSetter:'

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

4

9 回答 9

33

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

于 2013-10-24T14:48:46.867 回答
19

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

AppDelegate课堂上将其添加到didFinishLaunchingWithOptions

对于斯威夫特 2:

UINavigationBar.appearance().translucent = false

对于 Swift 3+:

UINavigationBar.appearance().isTranslucent = false
于 2016-07-25T10:52:57.310 回答
8

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

[[UINavigationBar appearance] setTranslucent:NO];
于 2015-08-12T04:29:19.943 回答
3

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

头文件:

#import <UIKit/UIKit.h>

@interface ABCNonTranslucentNavBar : UINavigationBar

@end

实现文件:

#import "ABCNonTranslucentNavBar.h"

@implementation ABCNonTranslucentNavBar

- (void)drawRect:(CGRect)rect
{
  [self setTranslucent:NO];
}

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

于 2013-09-19T00:03:04.570 回答
3

添加这个以防有人仍在与此作斗争。

你可以通过指定一个不存在的图像来欺骗它,这将使导航栏包括它的工具栏变得不透明

[[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];
于 2013-11-05T17:20:05.867 回答
2

我知道这很旧,但这可能对某人有用;

您可以使用一个类别,并在其中*设置属性[translucent][1]

@implementation UINavigationBar (MakeTranslucent)

-(void)willMoveToWindow:(UIWindow *)newWindow {
    [super willMoveToWindow:newWindow];


    self.translucent = NO;
}
@end
  • 我用过willMoveToWindow,不知道UAYOR这样是不是个好主意。
于 2014-10-04T20:08:40.930 回答
1

请参阅 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.
     */

正确的 Swift 4 解决方案是

UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().backgroundColor = .white
于 2018-10-08T14:56:04.037 回答
0

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

这里的 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 ;
于 2013-12-19T06:24:27.600 回答
-3

如果您不使用情节提要,而是使用 IB,请将 MainWindows.xib 的导航栏样式设置为 NOT translucent 并设置为颜色而不是清晰的颜色。

于 2013-11-10T16:19:45.447 回答