Change status bar style with animation
Asked Answered
D

3

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.

Decapod answered 24/2, 2017 at 16:47 Comment(2)
UIApplication.shared.statusBarStyle = .lightContent Have you used thisShenyang
This will not change the bar with animationDecapod
M
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()

Marga answered 24/2, 2017 at 16:57 Comment(5)
It is not being called from UIViewControllerDecapod
Where are you trying to set the status bar style?Marga
in viewWillAppearDecapod
Can you update your question with code so we can understand better?Marga
The problem was that I previously set View controller-based status bar appearance to NODecapod
S
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

Sibbie answered 24/2, 2017 at 17:13 Comment(1)
It is not being called from UIViewControllerDecapod
P
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.

Purulent answered 18/9, 2019 at 4:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.