简体   繁体   English

更改UINavigationBar的背景颜色

[英]Change the background color of UINavigationBar

I'd like to change the background color of an UINvaigationBar and I used tintColor property but it's not changing the color. 我想更改UINvaigationBar的背景颜色,并且使用了tintColor属性,但它没有更改颜色。 You can checkout the code below. 您可以在下面查看代码。

In my Appdelegate I create a UITabBarController 在我的Appdelegate中,我创建一个UITabBarController

tabbar = [[UITabBarController alloc] init];

Then I create a UINavigationController and attach my UITableViewController to it 然后我创建一个UINavigationController并将我的UITableViewController附加到它

UINavigationController *myNavigation = [[UINavigationController alloc] initWithRootViewController:myViewController];

Then attached the UINavigationControllers to my tabbar like this 然后像这样将UINavigationControllers附加到我的标签栏

[tabbar setViewControllers:viewControllers]; //viewControllers is an array of UINavigationControllers

I tried setting the property tintColor in myAppdelegate like this 我试图像这样在myAppdelegate中设置属性tintColor

[[myNavigation navigationBar] setTintColor:[UIColor redColor]];

But this didn't work as expected and so I tried the same in my ViewController 但这没有按预期工作,因此我在ViewController中尝试了相同的操作

 - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
        self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
        self.navigationItem.title = @"Test";
    }

And still nothing happens. 仍然没有任何反应。 Please checkout the image below to see how navigation bar looks now. 请查看下面的图片,以查看导航栏现在的外观。

在此处输入图片说明

Appreciate your help. 感谢您的帮助。

Cheers Jugs 干杯

As a work around you may can use image on place of tint color please check below code for same. 作为一种变通办法,您可以在色彩的位置使用图像,请检查以下代码是否相同。

if ([myNavigation respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
        [myNavigation setBackgroundImage:[UIImage imageNamed:BAR_IMAGE_NAME] forbarMetrics:UIBarMetricsDefault];
    }else {
        [myNavigation insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_IMAGE_NAME]] autorelease]  atIndex:0];
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM