iOS 設置導航條、狀態條透明和背景顏色的解決方案

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];
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容