0

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

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

以及在 ios6 和 ios7 中更改导航栏背景颜色的方法。

谢谢!!

4

5 回答 5

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;
    }
}
于 2014-09-25T10:03:45.857 回答
0

barTintColor= 适用于导航栏背景。此选项仅适用于 iOS 7。对于 iOS 6,您可以使用 tintColor。

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

开发者参考

于 2014-09-25T09:49:49.060 回答
0

试试这个...我已经参考了它支持 iOS6 和 iOS7 的这个链接

// 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]];
于 2014-09-25T09:47:25.410 回答
0

在 iOS6 中

[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];

在 iOS7 中

navigationController.navigationBar.barTintColor = [UIColor greenColor];

或者

[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];
于 2014-09-25T09:48:07.497 回答
0
self.navigationBar.barTintColor = [UIColor blueColor];
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.translucent = NO;

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

于 2014-09-25T09:44:33.813 回答