gpt4 book ai didi

iOS 将 UIAlertAction 移至屏幕顶部

转载 作者:行者123 更新时间:2023-11-28 14:05:32 25 4
gpt4 key购买 nike

我目前正在尝试添加一个包含 3 个文本字段的警报。我在消息中添加了多个\n 以使 UIAlertController 更大。但是,当键盘出现时,它会阻止确定和取消按钮。因此,我想知道是否有办法将 UIAlertController 移到屏幕上更高的位置以防止按钮被挡住。

    let alert = UIAlertController(title: "", message: "\n\n\n\n\n\n\n\n\n", preferredStyle: UIAlertController.Style.alert)
alert.view.addSubview(textField1)
alert.view.addSubview(textField2)
alert.view.addSubview(textField3)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default) { (_) in
//
}
let okAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default) { (_) in
//
}
alert.addAction(okAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: {})

最佳答案

UIAlertController包含 addTextField(configurationHandler: ((UITextField) -> Void)?您可以直接使用文本字段的属性,例如,您可以像下面这样使用

let alertController = UIAlertController(title: "", message: "\n\n\n\n\n\n\n\n\n", preferredStyle: .alert)
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter First Name"
}
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter Second Name"
}
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Enter Third Name"
}
let saveAction = UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in
let firstTextField = alertController.textFields?.first as! UITextField
let secondTextField = alertController.textFields![1] as UITextField
let thirdTextField = alertController.textFields?.last as! UITextField
print("firstName \(firstTextField.text), secondName \(secondTextField.text),thirdName \(thirdTextField.text)" )
})
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: { (action : UIAlertAction!) -> Void in })
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)

例如,您可以从 here 获得示例教程

关于iOS 将 UIAlertAction 移至屏幕顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53018856/

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