2015-07-10 56 views
0
InViewDidLoad 
    UIApplication.sharedApplication().statusBarStyle = .LightContent 

override func viewWillDisappear(animated: Bool) { 
     super.viewWillDisappear(animated) 

     UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default 

    } 

通過參考我已經實現的上述鏈接。但狀態欄顏色不會改變只有文本在狀態欄上正在改變。 Changing the Status Bar Color for specific ViewControllers using Swift in iOS8如何使用swift在特定視圖中更改狀態欄顏色?

回答

0

如果您的應用程序使用的是導航欄,則狀態欄顏色將更改爲與導航欄的顏色相匹配。

如果沒有導航欄,您可以在狀態欄後面放置一個20點高的視圖(例如,填充了顏色的某個視圖),這就是用戶將看到的顏色。

我發現了上述信息in this article

要迅速做到這一點,也許這樣做:

// make a view tall enough to fit under the status bar 
var statusBarView: UIView = UIView(frame: CGRectMake(0.0, 0.0, 320.0, 20.0)) 

// give it some color 
statusBarView.backgroundColor = UIColor.redColor() 

// and add it to your view controller's view 
view.addSubview(statusBarView) 
0

有狀態欄默認背後沒有獨立的後臺視圖。它實際上只是覆蓋在主視圖的頂部,所以背景採用了這種顏色。

所以,如果你有一個嵌入導航控制器的視圖,它將採用導航欄的顏色。如果不是,那麼它只會採用主視圖的顏色。

您可以創建一個單獨的視圖,位於狀態欄頂部的下方並設置其背景。正如Michael所說,以下代碼應該做到這一點,但我已經將寬度更改爲設備的寬度,因此它適用於所有情況。

var statusBarView: UIView = UIView(frame: CGRectMake(0.0, 0.0, UIScreen.mainScreen().bounds.width, 20.0)) 
statusBarView.backgroundColor = UIColor.redColor() 
view.addSubview(statusBarView) 
相關問題