iOS 将 UIAlertAction 移至屏幕顶部

iOS move UIAlertAction higher to top of screen

我目前正在尝试添加一个包含 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

获取示例教程