How to change Navigation Bar color in iOS 7?

2019-01-01 06:40发布

How do I change the Navigation Bar color in iOS 7?

Basically I want to achieve something like the Twitter Nav Bar (updated Twitter for iOS7 that is). I embedded-in a nav bar atop a view controller. All I want is to change the nav bar color to light blue along with the utility bar at the top. I can't seem to find an option in my storyboard.

18条回答
唯独是你
2楼-- · 2019-01-01 06:47

For swift change navigation bar color:

    self.navigationController?.navigationBar.barTintColor = UIColor.redColor()

change title font,size, color:

    self.title = "title"
    self.navigationController?.navigationBar.titleTextAttributes = [
        NSForegroundColorAttributeName : UIColor.whiteColor(),
        NSFontAttributeName : UIFont(name: "Futura", size: 30)!
    ]
查看更多
明月照影归
3楼-- · 2019-01-01 06:47
#define _kisiOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)


  if (_kisiOS7)
    {
        [[UINavigationBar appearance] setBarTintColor:[UIcolor redcolor]];
    }
    else
    {
        [[UINavigationBar appearance] setBackgroundColor:[UIcolor blackcolor]];
        [[UINavigationBar appearance] setTintColor:[UIcolor graycolor]];
    }
查看更多
妖精总统
4楼-- · 2019-01-01 06:48

To make Rajneesh071's code complete, you may also want to set the navigation bar's title color (and font, if you want) since the default behavior changed from iOS 6 to 7:

NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7)
{
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.translucent = NO;
    NSMutableDictionary *textAttributes = [[NSMutableDictionary alloc] initWithDictionary:mainNavController.navigationBar.titleTextAttributes];
    [textAttributes setValue:[UIColor whiteColor] forKey:UITextAttributeTextColor];
    self.navigationController.navigationBar.titleTextAttributes = textAttributes;
}
else
{
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
查看更多
千与千寻千般痛.
5楼-- · 2019-01-01 06:49

If you want to use a hex code, here is the best way to do so.

First, define this at the top of your class:

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

Then inside the "application didFinishLaunchingWithOptions", put this:

[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x00b0f0)];

Put you hex code in place of the 00b0f0.

查看更多
千与千寻千般痛.
6楼-- · 2019-01-01 06:50

In a navigation based application you can change color

NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
    self.navigationController.navigationBar.translucent = NO;
} else {
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];
}
查看更多
冷夜・残月
7楼-- · 2019-01-01 06:51

The behavior of tintColor for bars has changed in iOS 7.0. It no longer affects the bar's background.

From the documentation:

barTintColor Class Reference

The tint color to apply to the navigation bar background.

@property(nonatomic, retain) UIColor *barTintColor

Discussion
This color is made translucent by default unless you set the translucent property to NO.

Availability

Available in iOS 7.0 and later.

Declared In
UINavigationBar.h

Code

NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
    // iOS 7.0 or later   
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationController.navigationBar.translucent = NO;
}else {
    // iOS 6.1 or earlier
    self.navigationController.navigationBar.tintColor = [UIColor redColor];
}

We can also use this to check iOS Version as mention in iOS 7 UI Transition Guide

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
        // iOS 6.1 or earlier
        self.navigationController.navigationBar.tintColor = [UIColor redColor];
    } else {
        // iOS 7.0 or later     
        self.navigationController.navigationBar.barTintColor = [UIColor redColor];
        self.navigationController.navigationBar.translucent = NO;
    }

EDIT Using xib

enter image description here

查看更多
登录 后发表回答