13

i want to change the color of my device status bar if the internet is connected than the status bar color should turn Black and if the internet is not connected the color or status bar should turn Red so that it indicates wether internet is working or not during working with the application using SWIFT...help me out

Fatti Khan
  • 1,543
  • 1
  • 11
  • 19
  • 1
    System wide or just in your app, because as of iOS 7 the status bar is draw over you app, meaning you can easily place a view behind it and give that view ta background color to meet you requirements. – rckoenes Nov 25 '14 at 10:32
  • Possible duplicate of [Color text of status bar in XCode 6-b3 (Swift)](http://stackoverflow.com/questions/24746552/color-text-of-status-bar-in-xcode-6-b3-swift) – Caleb Kleveter Oct 23 '15 at 21:50
  • Might help anyone still looking for this. [Easy implementation of a colored view behind the status bar.](http://stackoverflow.com/questions/37092646/changing-statusbar-background-color-in-swift) – Ryan Daulton May 08 '16 at 20:20

9 Answers9

35

In your Info.plist you need to set "View controller-based status bar appearance" to a boolean value.

If you set it to YES then you should override preferredStatusBarStyle function in each view controller.

If you set it to NO then you can set the style in AppDelegate using:

UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
Allan
  • 7,039
  • 1
  • 16
  • 26
Nikita Khandelwal
  • 1,741
  • 16
  • 25
30
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationController?.navigationBarHidden =  true

    //Status bar style and visibility
    UIApplication.sharedApplication().statusBarHidden = false
    UIApplication.sharedApplication().statusBarStyle = .LightContent
    
    //Change status bar color
    let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
    if statusBar.respondsToSelector("setBackgroundColor:") {
        statusBar.backgroundColor = UIColor.redColor()
    }
    
}
Allan Spreys
  • 5,287
  • 5
  • 39
  • 44
Alvin George
  • 14,148
  • 92
  • 64
  • This was exactly what I wanted. I did change it slightly though: `if let statusBar = UIApplication.sharedApplication().valueForKey("statusBar") as? UIView {// my stuff }` – ghostatron Aug 20 '16 at 00:56
  • This is what I was looking for!! Thanks! – Jayprakash Dubey Aug 23 '16 at 09:18
  • Not sure where to put this? – Mick Oct 24 '16 at 21:39
  • @Mick If you want to apply the style to all UIViewControllers you can put it in AppDelegate `application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)`. If not, this code is supposed to be in the viewWillAppear of your desired UIViewController. – emmics Jan 30 '17 at 22:39
6

Tested in Swift & iOS9

If you use Navigation Controllers, put this in your viewcontroller class:

override func viewDidLoad(){
    ...
    self.navigationController?.navigationBar.barStyle = .Black
}

Otherwise, override the preferredStatusBarStyle() in your UIViewController:

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return .LightContent
}

You could find more information here

Cody
  • 4,353
  • 4
  • 39
  • 42
3

For Swift 2.3

Try with these methods

// Get network status
class func hasConnectivity() -> Bool {
    let reachability: Reachability = Reachability.reachabilityForInternetConnection()
    let networkStatus: Int = reachability.currentReachabilityStatus().value
    return networkStatus != 0
}

// change status bar color
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.blueColor()
navigationBarAppearace.barTintColor = UIColor.blueColor()

tintColor attribute change the background color of the navigation bar

barTintColor attribute affect to the color of the

But if you want to change the status bar color at the runtime, I think the better way is adding a view behind your status bar.

Kevin Machado
  • 4,141
  • 3
  • 29
  • 54
3

For Swift 3

This should work for Xcode 8 and Swift 3

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}
Taichi Kato
  • 602
  • 9
  • 24
2

As @rckoenes commented as of iOS 7 the status bar is draw over your app. So you can put a view behind the status bar area (20px from top - height of status bar) and can control it's background colour as per internet connection status changes, there is no other option you to change status bar colour.

Yuvrajsinh
  • 4,536
  • 1
  • 18
  • 32
  • i have seen an application working with status bar on internet connectivity , without the view behind status bar, it work with the color or status bar in obj c but for swift i have to search that out – Fatti Khan Nov 25 '14 at 12:34
  • @FattiKhan If you have any app for reference you can mention it and if you can do it with obj c then definitely you can do with swift. If you have any reference code then share that also. – Yuvrajsinh Nov 25 '14 at 12:43
  • in some cases you will also have to set the opacity to 0.9 to get the color to match with the nav bar – mjmayank Jul 31 '15 at 18:45
2

// Within your AppDelegate.swift in didFinishLaunchingWithOptions: UINavigationBar.appearance().barTintColor = UIColor.greenColor()

//Optionally, if you need a specific color, how you do it with RGB:
UINavigationBar.appearance().barTintColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

                                or 

In your Info.plist you need to set "View controller-based status bar appearance" to a boolean value.

 UIApplication.sharedApplication().statusBarStyle = .LightContent
Ashok Londhe
  • 1,491
  • 11
  • 29
Shanmugasundharam
  • 2,082
  • 23
  • 32
  • Are there any additional settings that needs to be made? This doesn't work for me. – Andrej Jan 04 '16 at 20:20
  • 1
    @Andrej Open your info.plist and insert a new key named "View controller-based status bar appearance" to NO then add this code in UIApplication.sharedApplication().statusBarStyle = .LightContent // AppDelegate.swift in didFinishLaunchingWithOptions: – Shanmugasundharam Jan 05 '16 at 05:52
  • Setting the custom color (eg. `UIColor.greenColor()` ) doesn't work. However I can confirm that setting to predefined style `.LightContent` works ok. – Andrej Jan 05 '16 at 13:37
  • Aha, sorry. Only now I've noticed that the custom color can be set only to `UINavigationBar`, but not to the status bar. The question title is about the status bar only. – Andrej Jan 05 '16 at 13:40
0

To have white text in black status bar: Switch View controller-based status bar appearance to NO in Info.plist In AppDelegate.swift add let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView statusBar.backgroundColor = UIColor.black in didFinishLaunchingWithOptions

Anup G Prasad
  • 246
  • 2
  • 4
  • 14
-1
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Rahul Patel
  • 1,822
  • 12
  • 21