2014-11-06 57 views
0

我知道有一些關於此問題的問題,但沒有人幫助我解決特定問題。我想在整個屏幕上方顯示自定義模式,但我想保持Apple狀態欄可見。對於模式,我使用一個UIView進行調光效果,另一個用於用戶與之交互的實際視圖。然後將整個模態作爲子視圖添加到當前視圖中並顯示在前面。基本上,我試圖複製UIAlertView的行爲,除了自定義視圖。下面是我的一些代碼:在iOS中顯示UIView在蘋果狀態欄上8

var modalView:UIView = UIView(frame: self.view.window!.bounds) 
modalView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.66) 
modalView.addSubview(customView)  

self.view.window!.addSubview(modalView) 
modalView.window!.windowLevel = UIWindowLevelStatusBar + 1 

self.bringSubviewToFront(modalView) 

以上,customView是用戶與之交互的UIView

這很好,但由於某些原因,即使狀態欄的樣式設置爲LightContent,Apple狀態欄上的文本也會消失。從代碼中可以看出,我沒有觸及狀態欄。

我想讓狀態欄上的文字變暗,就像屏幕的其他部分一樣,我目前卡住了。有沒有人有任何關於如何獲得理想行爲的見解?

任何幫助將不勝感激!

+1

你可以嘗試去一個水平和執行機制的一個新的UIWindow內。下面是這種方法的一些注意事項:http://stackoverflow.com/q/8232398/1442620 – 2014-11-06 14:38:17

回答

4

EDIT(2015年11月16日)

我從this線程複製我的回答如下:在iOS的

這個答案休息8.3+和iOS 8的可能是以前的版本我不推薦任何人都使用它,特別是因爲它不能保證在未來的iOS版本中工作。

我的新解決方案使用UIViewController的標準presentViewController方法。我初始化一個UIViewController,添加我的自定義模態View作爲UIViewController的子視圖,然後可選地使用約束來定位模態View。奇蹟般有效!


原來的答案

繼安娜的建議,我把它用的UIWindow代替UIView.這是我的新代碼的工作:

var modalWindow:UIWindow = UIWindow(frame: self.view.window!.bounds) 

modalWindow.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.66) 
modalWindow.hidden = false 
modalWindow.windowLevel = (UIWindowLevelStatusBar + 1) 

modalWindow.addSubview(customView)  

modalWindow.makeKeyAndVisible() 

它現在的作品與之前完全相同, Apple狀態欄上的文字也會變暗。非常感謝您的幫助!

+0

只是一個快速提示:要隱藏窗口,請將'modalWindow'設置爲'nil',如下所示: 'modalWindow =無' – Alexander 2014-11-07 13:49:15

+0

你設法處理這個臨時uiwindow的旋轉?請檢查我的問題http://stackoverflow.com/questions/26976494/ios-8-7-uiwindow-with-uiwindowlevelstatusbar-rotation-issue – 2014-11-18 07:54:04

+0

應該在iOS 9中工作嗎? – zzzel 2015-11-16 21:49:07

4

更新2017年12月•斯威夫特4•iOS的10.0+

我簡要地嘗試接受答案的編輯解決方案,但我不能輕易使整個的viewController只是一個UIView旗幟。但是,通過訪問UIApplication中的statusBar值,我能夠將UIView作爲子視圖添加到statusBarView這會將UIView置於狀態欄的頂部。據我所知,這是一個相對穩定的解決方法,適用於最新的幾個iOS版本。

extension UIApplication { 

    /// Returns the status bar UIView 
    var statusBarView: UIView? { 
     return value(forKey: "statusBar") as? UIView 
    } 

} 

使用

let customBannerView = CustomStatusView(with: message) 
UIApplication.shared.statusBarView?.addSubview(customBannerView)