我目前正在尝试添加一个包含3个textField的警报。我在消息中添加了多个\ 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: {})
答案 0 :(得分:1)
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获得了示例教程