首页 > 解决方案 > 如何使按钮在 alertController 中可见?

问题描述

我在弄清楚这一点时遇到了一些问题。我在警报控制器中看不到取消按钮和确定按钮,它位于文本视图后面。我使用文本视图,以便有更多的空间可以写。

在此处输入图像描述

这是我写的代码。

@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)

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

标签: xcodeuialertcontroller

解决方案


用于.addTextField向警报添加文本字段。简单地添加子视图并不像您想要的那样工作,并且也明确不鼓励

重要的

UIAlertController 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。

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


推荐阅读