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