简体   繁体   English

如何在iOS 7中更改NavigationBar的背景颜色

[英]How to change the background color of NavigationBar in iOS 7

I'm new to Objective-C, today I tried to change color of my Navigation Bar and this works with this code: 我是Objective-C的新手,今天我尝试更改导航栏的颜色,并且此代码适用于此代码:

appDelegate.m : appDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0xf4f4f4)];

    return YES;
}

Now I try change specific ViewController navigation bar and this do not work. 现在,我尝试更改特定的ViewController导航栏,但这不起作用。

ViewController2.m: ViewController2.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x363636)];
    self.navigationController.navigationBar.translucent = NO;
}

When I enter this ViewContoller his Navigation color is f4f4f4 and when I go other ViewController and come back it changes the color to 363636 . 当我输入此ViewContoller时,其导航颜色为f4f4f4 ,当我转到其他ViewController并返回时,其颜色更改为363636

Why this do not work in first time? 为什么这在第一时间不起作用? Can somebody explain this to me.. 有人可以向我解释一下吗。

(Sorry About my English, and thank you.) (对不起,我的英语,谢谢。)

when you use 当你使用

[UINavigationBar appearance] 

it changes all of the navbars. 它会更改所有导航栏。 in your viewcontroller, just do 在您的视图控制器中,只需执行

[self.navigationBar setBarTintColor:UIColorFromRGB(0x363636)];

You can implement what Nathanael said in the -(void) viewDidLoad method of the view controller. 您可以在视图控制器的-(void) viewDidLoad方法中实现Nathanael所说的内容。 But if you're using the storyboard, you can also select the navigation item and change the tint color from the File inspector without having to code. 但是,如果您使用情节提要板,则还可以选择navigation item并从“ File inspector更改颜色,而无需编写代码。

在此处输入图片说明

使用导航栏的tintcolor属性更改颜色

you can try it, I change in my AppDelegate the navigation bar color, so all the app has the same color, but i have to change the navigation bar color in a single view controller so I did it: 您可以尝试一下,我在AppDelegate中更改了导航栏颜色,因此所有应用程序都具有相同的颜色,但是我必须在单个视图控制器中更改导航栏颜色,所以我做到了:

in the AppDelegate 在AppDelegate中

let color = UIColor(red:0.24, green:0.72, blue:0.28, alpha:1.0)
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UINavigationBar.appearance().barTintColor = color

and in the view controller in the viewDidLoad I have this: 在viewDidLoad的视图控制器中,我有这个:

self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()

in the viewWillDissappear this: viewWillDissappear如下:

super.viewWillDisappear(animated)

    let color = UIColor(red:0.24, green:0.72, blue:0.28, alpha:1.0)
    self.navigationController?.navigationBar.barTintColor = color

It returns the navigation bar color to the same color 它将导航栏颜色恢复为相同的颜色

For iOS 10 you can use barStyle like this: 对于iOS 10,您可以使用如下所示的barStyle:

For white bar color: 对于白色条形:

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

And for black bar color: 对于黑条颜色:

self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

BarStyle need set in ViewDidLoad for normal animation work :) 需要在ViewDidLoad中设置BarStyle才能进行常规动画工作:)

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

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