当存在键盘时,使用 UITextView 向上移动 UIAlertController 样式警报

gio*_*o83 2 keyboard uitextview uialertview ios swift

我有一个带有 UITextView 的 AlertController。

当 UITexView 成为第一响应者时,alter 不会随着键盘向上移动。

这是我的代码:

@IBAction func showAlert(sender: AnyObject) {

    let alertController = UIAlertController(title: "Hello, I'm alert! \n\n\n\n\n\n\n", message: "", preferredStyle: .alert)

    let rect        =   CGRect(x: 15, y: 15, width: 240, height: 150)//CGRectMake(15, 50, 240, 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

    alertController.view.addSubview(textView)

    let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    let action = UIAlertAction(title: "Ok", style: .default, handler: { action in

        let msg = (textView.textColor == UIColor.lightGray) ? "" : textView.text

        print(msg!)

    })
    alertController.addAction(cancel)
    alertController.addAction(action)


    self.present(alertController, animated: true, completion: {

        textView.becomeFirstResponder()
    })
}
Run Code Online (Sandbox Code Playgroud)

这是我的结果:

在此处输入图片说明 有解决方案吗?

提前致谢

Eug*_*e K 5

只需 addTextField 然后将其删除

    alert.addTextField { field in
        field.translatesAutoresizingMaskIntoConstraints = false
        field.heightAnchor.constraint(equalToConstant: 0).isActive = true
    }

    let inCntrlr =  alert.childViewControllers[0].view!
    inCntrlr.removeFromSuperview()
Run Code Online (Sandbox Code Playgroud)

然后你可以添加你自己的观点。这是一个 结果