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
-
1System 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 Answers
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)

- 7,039
- 1
- 16
- 26

- 1,741
- 16
- 25
-
This only allows you the change the color of the status bar from light to dark. – rckoenes Nov 25 '14 at 10:59
-
-
6You can't make it orange or pink , unless you dip your iphone in orange or pink color paint respecvly :D – Nikita Khandelwal Nov 25 '14 at 11:04
-
But you can put view behind it and give that view any color you want. – rckoenes Nov 25 '14 at 12:14
-
this would rather just change the status from light to dark, i think you didn't get my point – Fatti Khan Nov 25 '14 at 12:36
-
Yes you can customize it.. putting a view behind it and can put any color u like.. But iOS dont have such native feature – Nikita Khandelwal Nov 25 '14 at 12:40
-
If you set "View controller-based status bar appearance" to NO, then you can add another entry to the plist: "Status bar style" with a value of UIStatusBarStyleLightContent instead of putting the code in the app delegate. – ghostatron Apr 16 '16 at 02:00
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()
}
}

- 5,287
- 5
- 39
- 44

- 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
-
-
-
@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
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

- 4,353
- 4
- 39
- 42
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.

- 4,141
- 3
- 29
- 54
-
-
It's my own method to convert *Hex* to `UIColor`. But doesn't matter, you can replace `uicolorFromHex:` by the `UIColor` you want ;) – Kevin Machado Sep 27 '16 at 09:14
-
I know it is your method but you should write here, I think Stackoverflow have be clear and correct answers. – Phd. Burak Öztürk Sep 28 '16 at 00:12
-
For Swift 3
This should work for Xcode 8 and Swift 3
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

- 602
- 9
- 24
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.

- 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
// 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

- 1,491
- 11
- 29

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

- 246
- 2
- 4
- 14
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)

- 36,322
- 27
- 84
- 93

- 1,822
- 12
- 21