Ios 在非UIViewController类中显示UIAlertController时,如何隐藏状态栏?
当用户点击非UIViewController类中的按钮时,我试图隐藏状态栏,但没有成功 我使用以下代码演示UIAlertController: jazzgil从以下答案中引用了这一点: 在我的UIButton操作中,我实现了以下内容:Ios 在非UIViewController类中显示UIAlertController时,如何隐藏状态栏?,ios,swift,uialertcontroller,Ios,Swift,Uialertcontroller,当用户点击非UIViewController类中的按钮时,我试图隐藏状态栏,但没有成功 我使用以下代码演示UIAlertController: jazzgil从以下答案中引用了这一点: 在我的UIButton操作中,我实现了以下内容: @IBAction func setImage(_ sender: UIBarButtonItem) { let alertView = UIAlertController(title: "Title", message: "Message", prefe
@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.ModalPresentationCaptureStatusBarAppearance=true以及alertView.setNeedsStatusBarAppearanceUpdate,但状态栏似乎总是出现
有人能指引我正确的方向吗
谢谢 希望这对你有帮助
要隐藏状态栏,请在显示alertview控制器之前在您的案例中调用此方法
func hideStatusBar() {
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar
}
func updateStatusBarToPreviousState() {
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal
}
要返回状态栏,请在关闭alertview控制器后调用此方法
func hideStatusBar() {
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar
}
func updateStatusBarToPreviousState() {
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal
}
请简单说明答案和使用方法,您必须调用方法hideStatusBar以隐藏状态栏,并在解除alertview控制器后显示回调用方法updateStatusBarToPreviousState。hideStatusBar工作,但当我在okAction中调用updateStatusBarToPreviousState时,状态栏永远不会重新出现??