如何更改ios 6和ios7中导航栏的背景颜色?

时间:2014-09-25 09:40:07

标签: ios iphone ipad uinavigationbar uibackgroundcolor

如何在iOS6和iOS7中更改导航栏的背景颜色?我想知道何时使用setBarTintColor:方法以及何时使用backgroundColor来更改导航栏的背景颜色。

请告诉我这两种方法的区别。

这是一种在ios6和ios7中更改导航栏背景颜色的方法。

谢谢!

5 个答案:

答案 0 :(得分:0)

self.navigationBar.barTintColor = [UIColor blueColor];
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.translucent = NO;

// barTintColor 设置背景颜色 // tintColor 设置按钮颜色

答案 1 :(得分:0)

试试这个......我已经提到它支持iOS 6和iOS7的link

// Uncomment to change the background color of navigation bar
     [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];

    // Uncomment to change the color of back button
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

    // Uncomment to assign a custom backgroung image
     [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg_ios7.png"] forBarMetrics:UIBarMetricsDefault];

    // Uncomment to change the back indicator image

    [[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back_btn.png"]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back_btn.png"]];


    // Uncomment to change the font style of the title

    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:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];

答案 2 :(得分:0)

在ios6中

[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];
在ios7中

navigationController.navigationBar.barTintColor = [UIColor greenColor];

[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];

答案 3 :(得分:0)

barTintColor =适用于导航栏背景。这个仅适用于iOS 7.对于iOS 6,您可以使用tintColor。

tintColor =适用于导航项和条形按钮项。

Developer Reference

答案 4 :(得分:0)

你可以使用

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];
}

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;
    }
}