1. 程式人生 > >iOS導航欄隱藏的情況下設定狀態列顏色

iOS導航欄隱藏的情況下設定狀態列顏色

背景介紹:

有的專案要求在tabbat管理的控制器內,有個介面是H5介面,一般H5介面有自己的導航欄,所以在切換到當前H5介面的時候,需要隱藏native(iOS端)的導航欄。

導航欄隱藏方式:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.delegate = self;
}

#pragma mark - UINavigationControllerDelegate
// 將要顯示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判斷要顯示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

這是個很好的設定導航欄隱藏的方式,但是當你興沖沖的以為完美解決問題的時候,新的問題就來了,你會發現狀態列不見了,導航欄的位置是20高度的白色,很難看。其實狀態列還是有的,只不過狀態列的背景顏色沒有了,這個時候需要給狀態列一個背景顏色,而我用的方式就是給狀態列的位置增加一個view。

    //設定狀態列顏色
    UIView *statusBarView = [[UIView alloc]   initWithFrame:CGRectMake(0, 0,    self.view.bounds.size.width, 20)];
    statusBarView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:statusBarView];

    //載入webview,frame的H要給20,因為有狀態列。
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT-20)];
這樣問題就完美解決了。