简体   繁体   English

如何更改导航栏的背景颜色和导航文本颜色

[英]how to change background color of the navigation bar and navigation text color

I want the background color of the navigation bar to be black and the text on the navigation bar to be white. 我希望导航栏的背景颜色为黑色,而导航栏上的文本为白色。

Everything looks fine on my simulator as shown below: 如下所示,在我的模拟器上一切正常:

在此处输入图片说明

However, when I test it on the actual device w/ 7.1, even though the background color of navigation bar is orange, the title of the screen is black. 但是,当我在带有7.1的实际设备上对其进行测试时,即使导航栏的背景色为橙色,屏幕的标题也为黑色。

This is what I'm doing in my application delegate: 这是我在应用程序委托中所做的事情:

@window.rootViewController = UINavigationController.alloc.initWithRootViewController(main_controller)
@window.rootViewController.navigationBar.barTintColor = '#DF533B'.to_color
@window.rootViewController.navigationBar.translucent = true
@window.rootViewController.navigationBar.tintColor = UIColor.whiteColor
@window.rootViewController.navigationBar.setTitleTextAttributes({
    UITextAttributeTextColor => UIColor.whiteColor
})

my deployment_target is 7.0 and app.sdk_version is 7.1 我的deployment_target7.0app.sdk_version7.1

Should I be putting this code in viewDidLoad of all my controllers? 我是否应该将此代码放入所有控制器的viewDidLoad中?

Not familiar with RubyMotion, but you should use the UIAppearance proxy: 不熟悉RubyMotion,但是您应该使用UIAppearance代理:

[[UINavigationBar appearance] setBarTintColor:[self navBarColor]];

[[UINavigationBar appearance] setTintColor:[self navBarTitleColor]];

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [self navBarTitleColor], NSFontAttributeName : [self titleFont]}];

You should do this before you load your main window in your AppDelegate. 在将主窗口加载到AppDelegate中之前,应执行此操作。

只是要添加到对我来说似乎正确的Moby答案中,请确保将其添加到应用程序委托的didFinishLaunchingWithOptions中。

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

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