Search code examples
iosios7rubymotion

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:

enter image description here

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.

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

Should I be putting this code in viewDidLoad of all my controllers?


Solution

  • Not familiar with RubyMotion, but you should use the UIAppearance proxy:

    [[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.