2017-12-25 12 views
1

黒のステータスバーを白いテキストで表示したい。ビューコントローラがナビゲーションコントローラに組み込まれている場合は、私が使用 一般的なアプローチは、私がUINavigationControllerのステータスバーの背景をカスタマイズする

を行う光スタイルのステータスバーにカスタム黒のビューを作成するには、ビューコントローラ

CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height; 
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusBarHeight)]; 
view.backgroundColor = [UIColor blackColor]; 
[self.view addSubview:view]; 

の一番上に追加する必要があります

CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height; 
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, [UIScreen mainScreen].bounds.size.width, statusBarHeight)]; 
view.backgroundColor = [UIColor blackColor]; 
[self.navigationController.navigationBar addSubview:view]; 

これは非常にエレガントな解決策ではないようですが、おそらくもっと良いアプローチがありますか?

答えて

2

キーウィンドウでビューを追加することで、それを行うことができます。あなたのアプリがUINavigationControllerの下で実行されているのか、UIViewControllerの下で実行されているのかは関係ありません。私は以下のようにUIStatusBarStyleの拡張を行いました。

extension UIStatusBarStyle { 
    static func setbackground() { 
     let view = UIView.init(frame: UIApplication.shared.statusBarFrame) 
     view.backgroundColor = UIColor.black 
     UIApplication.shared.keyWindow?.addSubview(view) 
    } 
} 

このメソッドを、メソッドdidFinishLaunchingWithOptionsからのappdelegateで呼び出します。また

UIStatusBarStyle.setbackground() 

、あなたのInfo.plistファイルに設定したことを確認してください

View controller-based status bar appearance = NO 
Status bar style = Opaque black style 
関連する問題