2017-07-16 74 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