Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Status Bar Style - Swift 3 - change at any time

I'm finding it hard to change the status bar style programatically.

I see how to statically set it for each ViewController using a combo of (in ViewController.swift):

override var preferredStatusBarStyle: UIStatusBarStyle {
    return UIStatusBarStyle.default
}

and (in info.plist):

View controller-based status bar appearance = YES

...

I'm looking to change it whenever I want!

like image 478
Chris Allinson Avatar asked Dec 08 '22 21:12

Chris Allinson


2 Answers

Found the answer after quite a lot of digging!

Set (in info.plist):

View controller-based status bar appearance = NO

and remove the (in ViewController.swift):

override var preferredStatusBarStyle: UIStatusBarStyle {
    return UIStatusBarStyle.default
}

...

Now you can use (in ViewController.swift):

UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)

And, to initially set the style for each ViewController, use viewDidAppear:

override func viewDidAppear(_ animated: Bool) {
    UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: false)
}
like image 122
Chris Allinson Avatar answered Dec 24 '22 19:12

Chris Allinson


swift 3

1.Change in info.plist the row View controller-based status bar appearance and set it to NO

2.Change in appDelegate.swift in didFinishLaunchingWithOptions

UIApplication.shared.statusBarStyle = .lightContent
like image 41
Umesh Verma Avatar answered Dec 24 '22 19:12

Umesh Verma