【问题标题】:Swift - How to stop the keyboard from dismissing when an alert shows upSwift - 如何在出现警报时阻止键盘关闭
【发布时间】:2021-03-28 01:08:56
【问题描述】:

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

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

override var disablesAutomaticKeyboardDismissal: Bool { return true }

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

【问题讨论】:

  • 警告对话又名UIAlertController?
  • 是的,抱歉,UIAlertController
  • 然后显示一条警报消息,其中包含您自己的 UIView 对象,而不是使用 UIAlertController

标签: ios swift user-interface keyboard uialertcontroller


【解决方案1】:

使用以下解决方案修复,从这个答案修改 - 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)
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 2017-11-22
    相关资源
    最近更新 更多