Zack Shapiro Zack Shapiro -4 years ago 134
Swift Question

Changing status bar style mid-scroll

Since

setStatusBarStyle
was deprecated in iOS 10, is there a way to change my status bar from white to black or vice versa while doing something like scrolling through a scroll view?

I see the
preferredStatusBarStyle
property on
UIViewController
but I'd like something more granular that I can control while a user uses the view controller itself.

Answer Source

if you override preferredStatusBarStyle and then call setNeedsStatusBarAppearanceUpdate() when you need, that should work the same way.

class MyViewController: UIViewController {

	var currentStyle = UIStatusBarStyle.default

	override var preferredStatusBarStyle: UIStatusBarStyle {
        return currentStyle
    }
    // ...

	// here are the actions that change the status bar
	func myFunction(){
		// ...
		// condition that would determine the preferred style
		currentStyle = .lightContent
		setNeedsStatusBarAppearanceUpdate()
	}

}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download