gpt4 book ai didi

ios - 如何在 UIAlert 显示时显示键盘 带有自定义文本标签 View 的 UIAlert

转载 作者:行者123 更新时间:2023-11-28 10:03:36 26 4
gpt4 key购买 nike

UIAlertController 与自定义 View 和 UITextField 一起出现时如何显示键盘?我的意思是我想在用户不触摸警报 View 中的 UITextField 的情况下自动显示键盘。

我的代码如下所示,用于发出警报。

func callAlertConfirmation() {
let vc = UIViewController()
vc.preferredContentSize = CGSize(width: 250, height: 70)

let textBookmark = UITextField(frame: CGRect(x: 10, y: 0, width: 230, height: 40))
textBookmark.placeholder = "Typing folder name"
textBookmark.font = UIFont.systemFont(ofSize: 15)
textBookmark.textColor = UIColor.black
textBookmark.borderStyle = UITextField.BorderStyle.roundedRect
textBookmark.autocorrectionType = UITextAutocorrectionType.no
textBookmark.keyboardType = UIKeyboardType.alphabet
textBookmark.returnKeyType = UIReturnKeyType.done
textBookmark.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
textBookmark.textAlignment = NSTextAlignment.left
textBookmark.clearButtonMode = .whileEditing
vc.view.addSubview(textBookmark)

let alert = UIAlertController(title: "Create New Folder", message: nil, preferredStyle: .alert)
alert.setValue(vc, forKey: "contentViewController")

let actOKButton = UIAlertAction(title: "OK", style: .default) { (_) -> Void in
// action When User Okay
}
alert.addAction(actOKButton)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.preferredAction = actOKButton
present(alert, animated: true)
}

当我调用

callAlertConfirmation

我得到的结果如下图所示: enter image description here

但是我想调用的时候如下图

callAlertConfirmation

enter image description here

但是当我使用

alert.addTextField

当出现警报时,我会自动显示键盘。

提前致谢。

最佳答案

什么

正如@C4747N 已经说过的,你需要打电话

.becomeFirstResponder()

何时

您想在显示警报时调用此方法:

present(alert, animated: true) {
textBookmark.becomeFirstResponder()
}

你想要“阅读”的方式是这样的:

Present the alert and once you're done execute the completion body (make the alert's textField first responder)

关于ios - 如何在 UIAlert 显示时显示键盘 带有自定义文本标签 View 的 UIAlert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57145995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com