2017-11-12 4 views
1

特定のビューコントローラでステータスバーの色を変更したい。iPhoneでアプリケーションを切り替えるときにステータスバーの色を変更する際の問題

StackOverFlowの回答によると、私はそれを達成しました。

問題があります。 iPhoneでアプリケーションを切り替えると、色がフェードアウトして初期状態に戻ります。

問題ありません。ステータスバーに注意してください。

enter image description here

OKではありません。ステータスバーに注意してください。

enter image description here 私はそれを理解できません。私が試しコード:

  1. UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; 
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { 
        statusBar.backgroundColor = [UIColor redColor ]; 
    } 
    

、ステータスバーに挿入サブビューを statusBar.backgroundColorを設定します。

UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; 
UIView * backgroundColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 20) ]; 
backgroundColorView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
backgroundColorView.backgroundColor = [UIColor redColor ]; 
[statusBar.subviews.firstObject insertSubview: backgroundColorView atIndex:0]; 

だから層(CALayerの)を挿入することです。

私はブレークポイントで解析しようとしました。

-アプリがアクティブで、[ホーム]ボタンをダブルクリックしてアプリを切り替えると、その方法は- (void)viewWillDisappear:(BOOL)animatedと呼ばれません。それは私を少し混乱させます。

-アプリケーションのメソッド- (void)applicationWillResignActive:(UIApplication *)applicationでステータスバーの背景色を変更しようとしましたが、機能しません。どうしてか分かりません。

Githubのソースコードから、ランタイムでOKです。私の会社はランタイムを好きではありません。

他の方法がありますランタイムなし

そして、ランタイムがiPhoneの切り替えアプリケーションモードとどのように関係しているのか分かりません。

主な質問は、ランタイムなしで解決することです。より多くの説明が歓迎されます。私はそれが簡単だと思います、何が恋しいですか?

多くの進歩に感謝します。スウィフト4用

+1

アプリがアクティブでない間は、ステータスバーの色を変更できません。アプリケーション切り替えウィンドウでは、アプリケーションがウィンドウ上のアクティブなアプリケーションではないため、OSはステータスバーの色を制御します。だから私はあなたがしようとしていることは可能だとは思わない。 –

+0

ご返信ありがとうございます。 [このリポジトリ](https://github.com/wangrui460/WRNavigationBar)は実行時には問題ありません。そして、app * meetup *もそれをより複雑にしました。 – dengApro

+0

あなたは正しいです、私は** View **、 – dengApro

答えて

1

回答:

そしてそれは、Objective-Cのバージョンの回答navigationViewController

class ViewController: UIViewController { 

    let statusBarBgView = {() -> UIView in 
     let statusBarWindow: UIView = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView 
     let statusBarBgView = UIView(frame: (statusBarWindow.statusBar?.bounds)!) 
     return statusBarBgView 
    }() 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     UIApplication.shared.statusBarStyle = .lightContent 
     let navigationBar = self.navigationController?.navigationBar 
     self.statusBarBgView.backgroundColor = UIColor.red 
     navigationBar?.superview?.insertSubview(self.statusBarBgView, aboveSubview: navigationBar!) 
    } 


    override func viewWillDisappear(_ animated: Bool) { 

     self.statusBarBgView.removeFromSuperview() 
     super.viewWillDisappear(animated) 
    } 

} 

extension UIView { 
    var statusBar: UIView? { 
     return value(forKey: "statusBar") as? UIView 
    } 
} 

によって管理viewControllersの状況に合った:ショー

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear: animated]; 
    [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent; 
    UINavigationBar *navigationBar = self.navigationController.navigationBar; 
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; 
    self.statusBarBgView = [[UIView alloc ] initWithFrame: statusBar.bounds ]; 
    self.statusBarBgView.backgroundColor = [UIColor redColor ]; 
    [navigationBar.superview insertSubview: self.statusBarBgView aboveSubview: navigationBar]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [self.statusBarBgView removeFromSuperview ]; 
    [super viewWillDisappear:animated]; 
} 

ステータスバーOS情報は、UIWindowで、アプリケーションスイッチャウィンドウにあるときにOSによって制御されます。

UIView *statusBar = [[[UIApplication sharedApplication] 
valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; 

だから、アプリスイッチャーウィンドウにする場合、ビューを変更することにより、ステータスバーの位置の背景色を調整するためにOKです。

+0

を変更することによってステータスバーの位置の背景色を調整します誰かが速いバージョンを書いてください! – Alfi

+1

更新しました。あなたが好きだと思う – dengApro

関連する問題