簡體   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