如何在 iOS6 和 iOS7 中更改导航栏的背景颜色?我想知道何时使用setBarTintColor:
方法以及何时使用backgroundColor
更改导航栏的背景颜色。
请告诉我这两种方法的区别。
以及在 ios6 和 ios7 中更改导航栏背景颜色的方法。
谢谢!!
如何在 iOS6 和 iOS7 中更改导航栏的背景颜色?我想知道何时使用setBarTintColor:
方法以及何时使用backgroundColor
更改导航栏的背景颜色。
请告诉我这两种方法的区别。
以及在 ios6 和 ios7 中更改导航栏背景颜色的方法。
谢谢!!
您可以使用
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;
}
}
试试这个...我已经参考了它支持 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]];
在 iOS6 中
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];
在 iOS7 中
navigationController.navigationBar.barTintColor = [UIColor greenColor];
或者
[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];
self.navigationBar.barTintColor = [UIColor blueColor];
self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.translucent = NO;
// barTintColor设置背景颜色 // tintColor设置按钮颜色