iOS将UIAlertAction移到更高的屏幕顶部



我目前正在尝试添加一个包含3个textFields的警报。我在消息中添加了多个,以便使UIAlertController更大。但是,当键盘出现时,它会阻止"确定"one_answers"取消"按钮。因此,我想知道是否有办法将UIAlertController在屏幕上移动得更高,以防止按钮被阻挡。

let alert = UIAlertController(title: "", message: "nnnnnnnnn", 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: "nnnnnnnnn", 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)

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

最新更新