简体   繁体   English

如何改变导航栏标题的正面颜色

[英]how to change front color of navigation bar title

I am using navigation controller in my application and want to change title color of navigationBar. 我在我的应用程序中使用导航控制器,并希望更改navigationBar的标题颜色。

I am doing so using below code 我正在使用下面的代码

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor],UITextAttributeTextColor,nil];
    [self.navController.navigationBar setTitleTextAttributes:dic];

One more thing I am using ARC xcode 4.2 and this code is placed on appdelegate only 我使用ARC xcode 4.2还有一件事,这个代码只放在appdelegate上

It is working fine in ios 4+ but not working on below versions. 它在ios 4+中运行良好,但不适用于以下版本。

Please help me how to do this from single code on appdelegate 请帮助我如何从appdelegate上的单个代码执行此操作

I was just looking for the same thing and found Alex RR's easy solution using iOS 5: 我只是在寻找相同的东西,并使用iOS 5找到了Alex RR的简单解决方案:

NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIColor whiteColor],UITextAttributeTextColor, 
                                            [UIColor blackColor], UITextAttributeTextShadowColor, 
                                            [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];

Reference: iPhone Navigation Bar Title text color 参考: iPhone导航栏标题文字颜色

You can recreate the titleView like that in your viewcontroller: 您可以在viewcontroller中重新创建titleView:

UILabel * titleView = [[UILabel alloc] initWithFrame:CGRectZero];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0];
titleView.shadowColor = [UIColor colorWithWhite:1.0 alpha:1.0];
titleView.shadowOffset = CGSizeMake(0.0f, 1.0f);
titleView.textColor = [UIColor redColor]; // Your color here
self.navigationItem.titleView = titleView;
[titleView sizeToFit];
[titleView release];

the other parameters are those wich are used in the native titleView. 其他参数是原生titleView中使用的参数。

** **

Please look at Max Strater's answer below to have a more recent solution. 请查看Max Strater的答案,以获得更新的解决方案。

** **

在iOS 5之后,只需执行以下操作:

    nav1.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];

In iOS 7, just use: 在iOS 7中,只需使用:

self.navController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};

Change [UIColor whiteColor] with whatever text color you want. 使用您想要的任何文本颜色更改[UIColor whiteColor]

in iOS 7 this is how you can change the color of Navbar title/text: 在iOS 7中,您可以通过以下方式更改Navbar标题/文本的颜色:

UIColor *red = [UIColor colorWithRed:254.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0];

NSMutableDictionary *navBarTextAttributes = [NSMutableDictionary dictionaryWithCapacity:1];

[navBarTextAttributes setObject:red forKey:NSForegroundColorAttributeName ];

self.navigationController.navigationBar.titleTextAttributes = navBarTextAttributes;

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

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