繁体   English   中英

Swift - 如何在出现警报时阻止键盘关闭

[英]Swift - How to stop the keyboard from dismissing when an alert shows up

我有一个带有文本字段的简单视图,该文本字段在视图加载时成为第一响应者。 如果用户输入了错误的代码,则会弹出一个“哎呀”警报对话框,并且键盘关闭,并且在单击警报对话框上的选项时,键盘会再次出现,导致我的视图移动。

有没有办法阻止这个键盘 - 永远 - 解雇? 我尝试使用我在其他地方找到的这个:

override var disablesAutomaticKeyboardDismissal: Bool { return true }

但是它似乎并不能解决问题。 谁能给我一个提示? :) 谢谢!

使用以下解决方案修复,从这个答案修改 - https://stackoverflow.com/a/47068284/14815664

func displayError(message: String) {
    let controller = UIAlertController(title: "Oops!", message: message, preferredStyle: .alert)
    controller.addAction(UIAlertAction(title: "Dismiss", style: .default))
    
    guard let alertWindow = UIApplication.shared.windows.last,
          alertWindow.windowLevel == UIWindow.Level(rawValue: 10000001.0) else {
      navigationController.present(controller, animated: true, completion: nil)
      return
    }
    alertWindow.rootViewController?.present(controller, animated: true, completion: nil)
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM