UINavigationBar set tintcolor tested in iOS7 not w

2019-03-17 14:27发布

I have an app which have a UINavigationBar and I have set the tint color to black like this:

self.navigationController.navigationBar.tintColor = [UIColor blackColor];`

I have tested it in IOS 6 and it's black. However, it appears as the default navigation bar when I tried the same app in iOS 7.

As the title says, is it not working?

6条回答
唯我独甜
2楼-- · 2019-03-17 14:50

I used following code to change tint color of navigation bar in iOS7,I added this in app delegate "applicationDidFinishLaunch" method and its work fine for me :

/* ios 7 Change */
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x4B678B)];
        NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
        shadow.shadowOffset = CGSizeMake(0, 1);
        [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                               [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
                                                               shadow, NSShadowAttributeName,
                                                               [UIFont fontWithName:@"Helvetica Neue" size:21.0], NSFontAttributeName, nil]];
        // self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
        //[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
        [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

    }
查看更多
叼着烟拽天下
3楼-- · 2019-03-17 14:58

You need to set the barTintColor property.

You can specify a custom tint color for the navigation bar background using the Tint (barTintColor) field. The default background tint color is white.

From iOS7 docs: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/UINavigationBar.html#//apple_ref/doc/uid/TP40012857-UINavigationBar-SW1

查看更多
走好不送
4楼-- · 2019-03-17 15:03
    [UINavigationBar appearance].tintColor = [UIColor redColor];
    if ([[UINavigationBar appearance] respondsToSelector:@selector(setBarTintColor:)]) {
        [UINavigationBar appearance].barTintColor   = [UIColor redColor];
    }

//OR

    self.navigationController.navigationBar.tintColor = [UIColor redColor];
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBarTintColor:)]) {
        self.navigationController.navigationBar.barTintColor    = [UIColor redColor];
    }
查看更多
对你真心纯属浪费
5楼-- · 2019-03-17 15:09

Following code is working for me :

self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
查看更多
你好瞎i
6楼-- · 2019-03-17 15:11

Fernando's and sanjana's answers have the key, but I'll just add something to make it clearer and more obvious.

The navigation bar has two properties

  • tintColor
  • barTintColor

It's a bit misleading when you don't think in iOS 7 terms.

tintColor changes the color of the buttons on your navigation bar. To change the background color, you need to set the property barTintColor.

self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
self.navigationController.navigationBar.tintColor = [UIColor greenColor];

This code snippet will give you a white navigation bar with green buttons.

查看更多
Rolldiameter
7楼-- · 2019-03-17 15:11

Had the same issue, but solved it via storyboard file.

Try the following.

  1. Open your .storyboard file.
  2. Select the scene with navigation controller
  3. Select Navigation Bar item into Navigation Controller scene enter image description here

  4. Open the Utilities tab on the right side of XCode

  5. Open Attributes Inspector
  6. In "Navigation Bar" group there will be Bar Tint dropdown list. You can choose any color for a tint. enter image description here
查看更多
登录 后发表回答