1.将状态栏和导航条设置成透明
/**
* 将状态栏和导航条设置成透明
当translucent = YES,controller中self.view的原点是从导航栏左上角开始计算
当translucent = NO,controller中self.view的原点是从导航栏左下角开始计算
*/
- (void)showNavigationWithClearBG{
self.navigationController.navigationBar.translucent = YES;
// 将状态栏和导航条设置成透明
UIImage *image = [UIImage imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0]];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
2.设置导航背景
/**
* 设置导航背景
* color 颜色
*/
- (void)setNavigationBGWithColor:(UIColor *)color{
if(self.navigationController.navigationBar.translucent == NO){
[self.navigationController.navigationBar setTranslucent:YES];
}
// 将状态栏和导航条设置成透明
if (color == nil) {
color = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0];
}
UIImage *image = [UIImage imageWithColor:color];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
3.显示不透明导航
/**
* 显示不透明导航(默认白色背景)
*
* @param color
*/
- (void)setNavigationWithBGColor:(UIColor *)color
{
if (color) {
[self setStatusBarBGColorWithBlack:YES];
}else{
color = [UIColor whiteColor];
[self setStatusBarBGColorWithBlack:NO];
}
[self.navigationController.navigationBar setTranslucent:NO];
UIImage *image = [UIImage imageWithColor:color];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
4.设置状态条背景颜色
/**
* 设置状态条背景颜色
*
* @param isBlack yes黑色,no是白色
*/
- (void)setStatusBarBGColorWithBlack:(BOOL)isBlack{
if(isBlack){
// 状态条背景部分变成黑色
self.navigationController.navigationBar.barTintColor = kNavBarBGColor;
// 状态条前景部分字变成白色
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
}else{
// 状态条背景部分变成白色
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
// 状态条前景部分字变成黑色
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}
}