11
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)

I use this one to change status bar to light in all app. But now I need to change it in just one View Controller back to black. How can I do that?

MarkHim
  • 5,686
  • 5
  • 32
  • 64
Ivan A
  • 193
  • 1
  • 1
  • 13
  • Possible duplicate of [Changing the Status Bar Color for specific ViewControllers using Swift in iOS8](http://stackoverflow.com/questions/26956728/changing-the-status-bar-color-for-specific-viewcontrollers-using-swift-in-ios8) – Rohit Jan 28 '16 at 13:03

10 Answers10

13

Set View controller-based status bar appearance in your project.plist to NO

Use viewWillAppear and will viewWillDisappear to set and reset the statusBarStyle, while keeping a property with the previous statusBarStyle like this

let initialStatusBarStyle : UIStatusBarStyle

func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    initialStatusBarStyle = UIApplication.sharedApplication().statusBarStyle
    UIApplication.sharedApplication().setStatusBarStyle(.LightContent, animated: animated)
}

func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.sharedApplication().setStatusBarStyle(initialStatusBarStyle, animated: animated)
}
MarkHim
  • 5,686
  • 5
  • 32
  • 64
  • Hmm, but it should. what is going – MarkHim Jan 28 '16 at 13:23
  • 1
    It was my mistake. I missed your phrase "Set View controller-based status bar appearance in your project.plist to NO". Now it works. Thank You! – Ivan A Jan 28 '16 at 13:26
  • Glad it worked out, you might want to mark your question as answered then – MarkHim Jan 28 '16 at 13:33
  • @MarkHim when I use this and switch between two VC using tabbed navigation it doesnt work. http://stackoverflow.com/questions/38546787/swift-ios-trying-to-change-color-of-status-bar-on-certain-vcs – user2722667 Jul 27 '16 at 13:16
  • `UIApplication.shared.setStatusBarStyle(initialStatusBarStyle, animated: true)` was deprecated in iOS 9. You might want to update your code. Use `UIApplication.shared.statusBarStyle = initialStatusBarStyle `. – Cesare May 04 '17 at 15:54
13

Xcode 8.1, Swift 3 Solution with @IBDesignable

This solution is a little bit different:

  • Subclass of UIViewController to centralize logic
  • No code for viewDidLoad or viewDidDisappear
  • Uses @IBDesignable so you can set your status bar color in the Attributes Inspector on the Storyboard

Step 1 - Setup Info.plist File

Info.plist

Step 2 - Subclass UIViewController

import UIKit

@IBDesignable
class DesignableViewController: UIViewController {

    @IBInspectable var LightStatusBar: Bool = false

    override var preferredStatusBarStyle: UIStatusBarStyle {
        get {
            if LightStatusBar {
                return UIStatusBarStyle.lightContent
            } else {
                return UIStatusBarStyle.default
            }
        }
    }
}

Step 3 - Inherit from DesignableViewController

Change the code for your ViewController(s) from:

class ViewController: UIViewController {

To:

class ViewController: DesignableViewController {

Step 4 - Set your preference on the Storyboard

Select the ViewControllers on the Storyboard and go to the Attributes Inspector: Attributes Inspector

Step 5 - Run project and test

In my project I setup a Tab Bar Controller with 2 View Controllers and switch back and forth between the two. Seems to work OK for me. Light Status Bar Dark Status Bar

Mark Moeykens
  • 15,915
  • 6
  • 63
  • 62
8

Solved:
Swift 3.1

Just using this code in View Controller:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}
reza_khalafi
  • 6,230
  • 7
  • 56
  • 82
2

Swift 3

Set View controller-based status bar appearance in your project.plist to NO

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.shared.setStatusBarStyle(.default, animated: animated)
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    UIApplication.shared.setStatusBarStyle(.lightContent, animated: animated)
}
budiDino
  • 13,044
  • 8
  • 95
  • 91
scurioni
  • 2,344
  • 1
  • 18
  • 14
1

An Objective-C answer:

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
Mutawe
  • 6,464
  • 3
  • 47
  • 90
0
 let color = UIColor(red:0.00, green:0.60, blue:0.48,alpha:1.0)
        UINavigationBar.appearance().tintColor = UIColor.blue
        UINavigationBar.appearance().barTintColor = color

OR

self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
Amul4608
  • 1,390
  • 14
  • 30
0

In swit4 this working fine in my project based on navigation bar

   let app = UIApplication.shared
   let statusBarHeight: CGFloat = app.statusBarFrame.size.height

   let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
   statusbarView.backgroundColor = UIColor.red
   view.addSubview(statusbarView)
0

You can set status bar color using below code and its working for me

self.navigationController?.SetStatusBar(StatusBarbackgroundColor: ThemeBackColor, StatusTextColor: .black)

Thanks Happy Coding :)

Praful Argiddi
  • 1,152
  • 1
  • 8
  • 12
0

Swift 5,iOS 14,UIKit

Step 1: Add Status bar style

enter image description here

Step 2: Change in Info.Plist

Add View controller-based status bar appearance key with NO value

enter image description here

Step 3: In your base view controller add this extension

extension UIViewController
{
func setStatusBarColor(){
    if #available(iOS 13, *)
    {
        let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
        let statusBarFrame = window?.windowScene?.statusBarManager?.statusBarFrame
        let statusBar = UIView(frame: (statusBarFrame)!)
        statusBar.backgroundColor = .orange
        window?.addSubview(statusBar)
    } else {
        // ADD THE STATUS BAR AND SET A CUSTOM COLOR
        let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
        if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
            statusBar.backgroundColor = .orange
        }
        UIApplication.shared.statusBarStyle = .lightContent
    }
}

}

Step 4: In your view controller use this extension

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    setStatusBarColor()
}
Vinayak Bhor
  • 691
  • 1
  • 8
  • 20
-1

If you have derived your view controllers from a common view controller then, you can simply do it like this:

Step 1: Add this key to your app's info.plist file.

enter image description here

Step 2: override this in common view controller (or a ParentViewController).

override var preferredStatusBarStyle: UIStatusBarStyle {
    if self is YourChildViewController {
        return .lightContent
    }
    return .default
}

That's it! No more fancy things.

Hemang
  • 26,840
  • 19
  • 119
  • 186