如何使按钮在 alertController 中可见?

How to make the buttons visible in the alertController?

我在解决这个问题时遇到了一些问题。我在 alertcontroller 中看不到取消按钮和确定按钮,它们位于文本视图后面。我正在使用 textview,以便有更多 space 可以写。

这是我写的代码。

@IBAction func appltyButton(_ sender: Any) {

    let alertController = UIAlertController(title: "Write your message.", message: nil, preferredStyle: .alert)

    let rect        = CGRect(x: 15, y: 50, width: 240, height: 150.0)
    let textView    = UITextView(frame: rect)

    textView.font               = UIFont(name: "Helvetica", size: 15)
    textView.textColor          = UIColor.lightGray
    textView.backgroundColor    = UIColor.white
    textView.layer.borderColor  = UIColor.lightGray.cgColor
    textView.layer.borderWidth  = 1.0
    textView.text               = "Enter message here"
    textView.delegate           = self as? UITextViewDelegate

    alertController.view.addSubview(textView)

    let okAction = UIAlertAction(title: "OK", style: .default, handler: self.okHandler)
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

    alertController.addAction(okAction)
    alertController.addAction(cancelAction)
    self.present(alertController, animated: true)

我希望能够看到按钮。谢谢

使用 .addTextField 将文本字段添加到您的提醒中。简单地添加子视图并不能像您预期的那样工作,并且也是明确的 discouraged:

Important

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

或者,使用允许更多自定义的众多警报库之一,例如SDCAlertView.