Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change status bar style with animation

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.

like image 254
Danny Avatar asked Feb 24 '17 16:02

Danny


3 Answers

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()

like image 150
Naveed J. Avatar answered Oct 08 '22 22:10

Naveed J.


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

like image 23
Krunal Avatar answered Oct 09 '22 00:10

Krunal


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.

like image 44
Johno2110 Avatar answered Oct 09 '22 00:10

Johno2110