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

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

how to change front color of navigation bar title

iphoneobjective-ciosuinavigationbaruicolor

提问by Shivomkara Chaturvedi

I am using navigation controller in my application and want to change title color of 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 上的单个代码执行此操作

采纳答案by Zoleas

You can recreate the titleView like that in your 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 的回答以获得更新的解决方案。

**

**

回答by Max Strater

I was just looking for the same thing and found Alex R. R.'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 Navigation Bar Title 文字颜色

回答by Damien Romito

After iOS 5, just do this :

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

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

回答by Enrique

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]为您想要的任何文本颜色。

回答by grepit

in iOS 7 this is how you can change the color of Navbar title/text:

在 iOS 7 中,您可以通过以下方式更改导航栏标题/文本的颜色:

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;