How to Change the status bar color using ios with swift on internet reachability?
Solution 1
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)
Solution 2
override func viewWillAppear(animated: Bool) {
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()
}
}
Solution 3
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
Solution 4
For Swift 3
This should work for Xcode 8 and Swift 3
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Solution 5
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.
Fatti Khan
NSMutableDictionary *userDetail=[[NSMutableDictionary alloc]init]; [userDetail setValue:@"iOS application development" forKey:@"Job"]; [userDetail setValue:@"SAP ABAP, BsCS" forKey:@"Education"]; [userDetail setValue:@"Solve difficult problem" forKey:@"Passion"];
Updated on February 11, 2020Comments
-
Fatti Khan over 4 years
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
-
rckoenes over 9 yearsThis only allows you the change the color of the status bar from light to dark.
-
Nikita Khandelwal over 9 yearsyou can change it to black or white only
-
Nikita Khandelwal over 9 yearsYou can't make it orange or pink , unless you dip your iphone in orange or pink color paint respecvly :D
-
rckoenes over 9 yearsBut you can put view behind it and give that view any color you want.
-
Fatti Khan over 9 yearsi 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 over 9 yearsthis would rather just change the status from light to dark, i think you didn't get my point
-
Nikita Khandelwal over 9 yearsYes you can customize it.. putting a view behind it and can put any color u like.. But iOS dont have such native feature
-
Yuvrajsinh over 9 years@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.
-
mjmayank almost 9 yearsin some cases you will also have to set the opacity to 0.9 to get the color to match with the nav bar
-
Andrej over 8 yearsAre there any additional settings that needs to be made? This doesn't work for me.
-
Shanmugasundharam over 8 years@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:
-
Andrej over 8 yearsSetting the custom color (eg.
UIColor.greenColor()
) doesn't work. However I can confirm that setting to predefined style.LightContent
works ok. -
Andrej over 8 yearsAha, 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. -
ghostatron about 8 yearsIf 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 almost 8 yearsThis was exactly what I wanted. I did change it slightly though:
if let statusBar = UIApplication.sharedApplication().valueForKey("statusBar") as? UIView {// my stuff }
-
Jayprakash Dubey almost 8 yearsThis is what I was looking for!! Thanks!
-
Phd. Burak Öztürk over 7 yearswhere is the uicolorFromHex func?
-
Kevin Machado over 7 yearsIt's my own method to convert Hex to
UIColor
. But doesn't matter, you can replaceuicolorFromHex:
by theUIColor
you want ;) -
Phd. Burak Öztürk over 7 yearsI know it is your method but you should write here, I think Stackoverflow have be clear and correct answers.
-
Kevin Machado over 7 yearsMay be you're right, I updated the method with UIColor.
-
Mick over 7 yearsNot sure where to put this?
-
emmics over 7 years@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.