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
问题:
回答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)
回答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()
}
}
回答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
回答4:
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.
回答5:
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.
回答6:
For Swift 3
This should work for Xcode 8 and Swift 3
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
回答7:
// 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
回答8:
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
回答9:
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)