How to Change the status bar color using ios with swift on internet reachability?

48,339

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.

Share:
48,339
Fatti Khan
Author by

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

Comments

  • Fatti Khan
    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
    rckoenes over 9 years
    This only allows you the change the color of the status bar from light to dark.
  • Nikita Khandelwal
    Nikita Khandelwal over 9 years
    you can change it to black or white only
  • Nikita Khandelwal
    Nikita Khandelwal over 9 years
    You can't make it orange or pink , unless you dip your iphone in orange or pink color paint respecvly :D
  • rckoenes
    rckoenes over 9 years
    But you can put view behind it and give that view any color you want.
  • Fatti Khan
    Fatti Khan over 9 years
    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
    Fatti Khan over 9 years
    this would rather just change the status from light to dark, i think you didn't get my point
  • Nikita Khandelwal
    Nikita Khandelwal over 9 years
    Yes you can customize it.. putting a view behind it and can put any color u like.. But iOS dont have such native feature
  • Yuvrajsinh
    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
    mjmayank almost 9 years
    in some cases you will also have to set the opacity to 0.9 to get the color to match with the nav bar
  • Andrej
    Andrej over 8 years
    Are there any additional settings that needs to be made? This doesn't work for me.
  • Shanmugasundharam
    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
    Andrej over 8 years
    Setting the custom color (eg. UIColor.greenColor() ) doesn't work. However I can confirm that setting to predefined style .LightContent works ok.
  • Andrej
    Andrej over 8 years
    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.
  • ghostatron
    ghostatron about 8 years
    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
    ghostatron almost 8 years
    This was exactly what I wanted. I did change it slightly though: if let statusBar = UIApplication.sharedApplication().valueForKey("statusBar") as? UIView {// my stuff }
  • Jayprakash Dubey
    Jayprakash Dubey almost 8 years
    This is what I was looking for!! Thanks!
  • Phd. Burak Öztürk
    Phd. Burak Öztürk over 7 years
    where is the uicolorFromHex func?
  • Kevin Machado
    Kevin Machado over 7 years
    It's my own method to convert Hex to UIColor. But doesn't matter, you can replace uicolorFromHex: by the UIColor you want ;)
  • Phd. Burak Öztürk
    Phd. Burak Öztürk over 7 years
    I know it is your method but you should write here, I think Stackoverflow have be clear and correct answers.
  • Kevin Machado
    Kevin Machado over 7 years
    May be you're right, I updated the method with UIColor.
  • Mick
    Mick over 7 years
    Not sure where to put this?
  • emmics
    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.