2017-07-16 41 views
0

我想隱藏狀態欄,當用戶點擊非UIViewController類中的按鈕,但沒有成功。如何在非UIViewController類中呈現UIAlertController時隱藏狀態欄?

我使用下面的代碼來呈現UIAlertController

public extension UIAlertController 
{ 
    func show() 
    { 
     let win = UIWindow(frame: UIScreen.main.bounds) 
     let vc = UIViewController() 
     vc.view.backgroundColor = .clear 
     win.rootViewController = vc 
     win.windowLevel = UIWindowLevelAlert + 1 
     win.makeKeyAndVisible()  
     vc.present(self, animated: true, completion: nil) 
    } 
} 

它是從以下的答案必須由jazzgil引用:

ios - present UIAlertController on top of everything regardless of the view hierarchy

在我UIButton動作我實現瞭如下:

@IBAction func setImage(_ sender: UIBarButtonItem) 
{ 
    let alertView = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.alert) 

    // Create the alert's action button 
    let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action: UIAlertAction!) in 



    }) 

    let cancelAction = UIAlertAction(title: "CANCEL", style: .destructive, handler: nil) 

    alertView.addAction(okAction) 
    alertView.addAction(cancelAction) 

    alertView.show() 
} 

我試圖在擴展中添加以下功能:

override open var prefersStatusBarHidden: Bool 
{ 
    return true 
} 

然後設置alertView.modalPresentationCapturesStatusBarAppearance = true以及alertView.setNeedsStatusBarAppearanceUpdate()但狀態欄似乎總是出現。

有人可以引導我在正確的方向嗎?

謝謝!

回答

1

希望這對你有所幫助。

要隱藏狀態欄調用此方法

func hideStatusBar() { 
    UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar 
} 

並找回狀態欄調用此方法(解僱alertview控制器後)

func updateStatusBarToPreviousState() { 
    UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal 
} 
+0

請解釋(在你的情況介紹alertview控制器之前)回答以及如何使用 – Pangu

+0

只需調用方法'hideStatusBar'來隱藏狀態欄並在解除alertview控制器後顯示回調方法'updateStatusBarToPreviousState'。 –

+0

'hideStatusBar'工程,但當我在我的okAction中調用'updateStatusBarToPreviousState'時,狀態欄不會再出現? – Pangu