我试图在用户点击非UIViewController类中的按钮时隐藏状态栏,但没有成功。
我使用以下代码来展示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()
,但状态栏似乎总是出现。
有人可以指导我朝正确的方向发展吗?
谢谢!
答案 0 :(得分:1)
希望这对你有所帮助。
要隐藏状态栏,请调用此方法(在出现alertview控制器之前的情况下)
func hideStatusBar() {
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar
}
要取回状态栏,请调用此方法(在解除alertview控制器后)
func updateStatusBarToPreviousState() {
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal
}