xcode 更改 NavigationBar 颜色(背景色)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10864691/
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-09-15 00:28:11  来源:igfitidea点击:

Change NavigationBar color (background color)

iphoneobjective-cxcode

提问by user869305

I keep reading to change the default color on a navigationbar i just need to update the first method in the appdelegate to this

我一直在阅读以更改导航栏上的默认颜色,我只需要将 appdelegate 中的第一个方法更新为此

self.window.rootViewController.navigationController.navigationBar.tintColor = [UIColor whiteColor];

but it doesn't seem to work, so I tried setting it in the viewDidLoad method of the firstview also:

但它似乎不起作用,所以我也尝试在 firstview 的 viewDidLoad 方法中设置它:

self.parentViewController.navigationController.navigationBar.tintColor = [UIColor whiteColor];

This didn't work either. How can I change this?

这也不起作用。我怎样才能改变这个?

回答by DrummerB

Don't use self.parentViewController, but self:

不要使用self.parentViewController,但是self

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

回答by Isuru Jayathissa

When iphone 5 come we have to set both device type. So use this

当 iphone 5 到来时,我们必须设置两种设备类型。所以用这个

if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
    //iOS 5 new UINavigationBar custom background
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbg_ForiPhone5_Imagename.png"] forBarMetrics: UIBarMetricsDefault];
} else {
    [self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbg_ForOtherIphone_Imagename.png"]] atIndex:0];
}

回答by Dalorzo

In IOS7 you can try this:

在IOS7你可以试试这个:

[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];

You can follow these steps:

您可以按照以下步骤操作:

I created a new UINavigationControllerfor example UIDemoNavControllerresulting in:

我创建了一个新UINavigationController的例如UIDemoNavController导致:

- (void)viewDidLoad{
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];  
    [super viewDidLoad];
}

This is the full demo class:

这是完整的演示类:

#import "UIDemoNavController.h"

@interface UIDemoNavController()

@end

@implementation UIDemoNavController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {}
    return self;
}

- (void)viewDidLoad{
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

@end

回答by graver

Try self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
UIViewControllerclass has a property navigationController, which returns the navigation controller its embedded in no matter how deep, otherwise it returns nil.

Tryself.navigationController.navigationBar.tintColor = [UIColor whiteColor];
UIViewController类有一个属性navigationController,它返回嵌入的导航控制器,无论多深,否则返回nil