Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 603
Fatti Khan Avatar asked Nov 25 '14 10:11

Fatti Khan


People also ask

How do I change the color of a status bar in Swift?

Open your info. plist and set UIViewControllerBasedStatusBarAppearance to false . In the first function in AppDelegate. swift , which contains didFinishLaunchingWithOptions , set the color you want.

How do I change the color of my status bar on my Iphone?

Go to the Storyboard. Select the View and in the Attributes Inspector change the Background Color to Light Gray. Build and Run the Project. The default style of the status bar is dark content.

How can I change the color of my status bar?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar. Step 3: In your MainActivity, add this code in your onCreate method.


3 Answers

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()
    }

}
like image 170
Alvin George Avatar answered Oct 12 '22 23:10

Alvin George


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)
like image 35
Nikita Khandelwal Avatar answered Oct 12 '22 23:10

Nikita Khandelwal


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

like image 6
Cody Avatar answered Oct 12 '22 23:10

Cody