[英]How to Change the status bar color using ios with swift on internet reachability?
如果已連接互聯網,我想更改設備狀態欄的顏色,而不是狀態欄顏色應變為黑色,如果未連接互聯網,則顏色或狀態欄應變為紅色,以表明在此期間互聯網是否正常工作使用 SWIFT 處理應用程序...幫助我
在您的Info.plist
您需要將“基於視圖控制器的狀態欄外觀”設置為布爾值。
如果您將其設置為YES
那么您應該覆蓋每個視圖控制器中的preferredStatusBarStyle
函數。
如果將其設置為NO
則可以使用以下方法在AppDelegate
設置樣式:
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
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()
}
}
在 Swift 和 iOS9 中測試
如果您使用Navigation Controllers ,請將其放在您的 viewcontroller 類中:
override func viewDidLoad(){
...
self.navigationController?.navigationBar.barStyle = .Black
}
否則,覆蓋 UIViewController 中的preferredStatusBarStyle()
:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
你可以在這里找到更多信息
對於 Swift 2.3
試試這些方法
// 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
屬性改變導航欄的背景顏色
barTintColor
屬性對顏色的影響
但是如果您想在運行時更改狀態欄顏色,我認為更好的方法是在狀態欄后面添加一個視圖。
對於 Swift 3
這應該適用於 Xcode 8 和 Swift 3
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
正如@rckoenes 所評論的,從 iOS 7 開始,狀態欄被繪制在您的應用程序上。 因此,您可以在狀態欄區域后面放置一個視圖(距離頂部 20 像素 - 狀態欄的高度),並且可以根據互聯網連接狀態的變化控制它的背景顏色,沒有其他選項可以更改狀態欄顏色。
// 在你的 AppDelegate.swift 中 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
在您的 Info.plist 中,您需要將“基於視圖控制器的狀態欄外觀”設置為布爾值。
UIApplication.sharedApplication().statusBarStyle = .LightContent
要在黑色狀態欄中顯示白色文本:在Info.plist中將基於視圖控制器的狀態欄外觀切換為NO在 AppDelegate.swift 中添加let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView statusBar.backgroundColor = UIColor.black
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView statusBar.backgroundColor = UIColor.black
in didFinishLaunchingWithOptions
UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.