我想将导航栏的背景设置为黑色,并将其中的所有颜色设置为白色。
所以,我使用了这段代码:
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],
NSForegroundColorAttributeName,
[UIColor whiteColor],
NSForegroundColorAttributeName,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
NSForegroundColorAttributeName,
[UIFont fontWithName:@"Arial-Bold" size:0.0],
NSFontAttributeName,
nil]];
但是后退按钮文本颜色、箭头和条形按钮仍然具有默认的蓝色。如何更改这些颜色,如下图所示?
https://i.stack.imgur.com/xwOa0.jpg
UINavigationBar
的某些属性的行为与 iOS 7 不同。您可以在下图中看到:
https://i.stack.imgur.com/DtNbN.png
我想与您分享两个漂亮的链接。有关更多详细信息,您可以通过以下链接:
iOS 7 UI 转换指南。如何为 iOS 7 更新您的应用程序。
barTintColor 的 Apple 文档 说:
除非您将 translucent 属性设置为 NO,否则此颜色默认为半透明。
示例代码:
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[self.navigationController.navigationBar
setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
self.navigationController.navigationBar.translucent = NO;
https://i.stack.imgur.com/bCv2y.png
这个花了我大约半天的时间才弄清楚,但这对我有用。在初始化 navigationController 的 rootViewController 中,我将这段代码放在了我的 viewDidAppear 方法中:
//set bar color
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:85.0/255.0 green:143.0/255.0 blue:220.0/255.0 alpha:1.0]];
//optional, i don't want my bar to be translucent
[self.navigationController.navigationBar setTranslucent:NO];
//set title and title color
[self.navigationItem setTitle:@"Title"];
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]];
//set back button color
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
//set back button arrow color
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
我在此 here 上的完整帖子
Swift3,iOS 10
要全局分配颜色,在 AppDelegate didFinishLaunchingWithOptions
中:
let textAttributes = [NSForegroundColorAttributeName:UIColor.white]
UINavigationBar.appearance().titleTextAttributes = textAttributes
斯威夫特 4,iOS 11
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]
UINavigationBar.appearance().titleTextAttributes = textAttributes
Vin的回答对我很有用。以下是使用 Xamarin.iOS/MonoTouch 的 C# 开发人员的相同解决方案:
var navigationBar = NavigationController.NavigationBar; //or another reference
navigationBar.BarTintColor = UIColor.Blue;
navigationBar.TintColor = UIColor.White;
navigationBar.SetTitleTextAttributes(new UITextAttributes() { TextColor = UIColor.White });
navigationBar.Translucent = false;
斯威夫特/iOS8
let textAttributes = NSMutableDictionary(capacity:1)
textAttributes.setObject(UIColor.whiteColor(), forKey: NSForegroundColorAttributeName)
navigationController?.navigationBar.titleTextAttributes = textAttributes
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
要以正确的方式更改 UINavigationBar
标题的颜色,请使用以下代码:
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]];
UITextAttributeTextColor
在最新的 ios 7 版本中已弃用。请改用 NSForegroundColorAttributeName
。
如果您要更改标题文本大小和文本颜色,则必须更改 NSDictionary titleTextAttributes,它的 2 个对象:
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:13.0],NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName,
nil];
我认为以前的答案是正确的,这是做同样事情的另一种方式。我在这里与其他人分享它以防万一它对某人有用。这是在 ios7 中更改导航栏的文本/标题颜色的方法:
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;
似乎 iOS Settings
中的 Accessibility
控件几乎覆盖了您尝试对导航栏按钮执行的所有颜色操作。确保您将所有设置都设置为默认位置(将增加对比度、粗体文本、按钮形状等设置为关闭),否则您将看不到任何变化。完成后,所有颜色更改代码都开始按预期工作。您可能不需要将它们全部关闭,但我没有进一步追求。
斯威夫特 5/iOS 13
要更改控制器中标题的颜色:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
不定期副业成功案例分享
[self.navigationController.navigationBar setTitleTextAttributes:@{[UIFont fontWithName:@"YOURFONT" size:14], NSFontAttributeName}];