Swift ios tries to change the status bar color on some VCs

In my application, I have 4 ViewController, and in two of them I change the status bar from white to black as follows:

override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default } override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) UIApplication.sharedApplication().statusBarStyle = .LightContent } 

The problem is that if I switch between the two ViewController, both have the code above the status bar, it will first be changed to black, which is correct, but then it will change to white again when entering another ViewController.

How can I keep the status bar white on a specific ViewController?

+2
source share
2 answers

Try adding the following method to your VC. Use .default or .lightContent to change the color of the status bar. I tested with Xcode 8 and quick 3:

 override func preferredStatusBarStyle() -> UIStatusBarStyle { return UIStatusBarStyle.default; } 

I created a new tabbed application with Xcode 7.3.1 and swift 2.3. I have two tabs with classes related to FirstViewController and SecondViewController. In FirstViewController, I added the following method:

 override func preferredStatusBarStyle() -> UIStatusBarStyle { return UIStatusBarStyle.default; } 

And in SecondViewController, I changed the background to black, and I added the following method:

 override func preferredStatusBarStyle() -> UIStatusBarStyle { return UIStatusBarStyle.LightContent; } 

Finally, I added two buttons to the FirstViewController. One button represents the controller modulo, and the other button is present via push. When I presented the view in the format "preferredStatusBarStyle", but when I presented via push, I need to add the following line of code:

 self.navigationController?.navigationBar.barStyle = .Black 
0
source

If you really don't want to override delegate methods for preferredStatusBarStyle, you can still use:

 UIApplication.sharedApplication().statusBarStyle = .LightContent 

deleting:

 override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) UIApplication.sharedApplication().statusBarStyle = .LightContent } 

and just let the status bar be set by what happens in viewWillAppear. Obviously this is more error prone, but if your navigation is relatively linear, then this will be the easiest solution.

0
source

Source: https://habr.com/ru/post/1241715/


All Articles