5

Since UIApplication.shared.setStatusBarStyle(.default, animated: true) is deprecated from IOS9 is it possible to change status bar style with animation on push? I cannot find any description in docs.

Krunal
  • 77,632
  • 48
  • 245
  • 261
Danny
  • 3,975
  • 4
  • 22
  • 35

3 Answers3

10

It's now a variable you have to override:

override var preferredStatusBarStyle: UIStatusBarStyle
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation

Depending on when you update the status bar, you might also have to call setNeedsStatusBarAppearanceUpdate()

Naveed J.
  • 3,176
  • 2
  • 12
  • 11
3

If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your .plist file.

if you wan to set status bar style, at view controller level then follow these steps:

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
  2. In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate

  3. override preferredStatusBarStyle in your view controller.

-

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNeedsStatusBarAppearanceUpdate()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

Set value of .plist according to status bar style setup level. enter image description here

Krunal
  • 77,632
  • 48
  • 245
  • 261
1

To tackle the animation part the way I made it transition smoothly between .lightContent and .default was to use something similar to below each time you change it.

UIView.animate(withDuration: 0.2) {
    self.setNeedsStatusBarAppearanceUpdate()
}

Place that in your VC you want the status bar to animate and you'll have a nice smooth animation.

override var preferredStatusBarStyle: UIStatusBarStyle {
    return lightStatusBar ? .lightContent : .default
}

Above is a snippet of me changing the content based on a condition I have.

José
  • 3,112
  • 1
  • 29
  • 42
Johno2110
  • 1,071
  • 1
  • 13
  • 25