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

标签 ios swift keyboard uitextview uialertview

我有一个带有 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()
    })
}

这是我的结果:

enter image description here 有解决办法吗?

提前致谢

最佳答案

只需添加 TextField 然后将其删除

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

    let inCntrlr =  alert.childViewControllers[0].view!
    inCntrlr.removeFromSuperview()

然后您可以添加自己的 View 。这里有一个 result

关于ios - 当键盘存在时,使用 UITextView 向上移动 UIAlertController 样式警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41896083/

相关文章:

iOS完成 block 不返回控制

swift - NSGridview 的背景色

cocoa - 如何判断在 Dock 上放置过程中是否按下了修饰键

android - Android 键盘中的表情符号

命令行 C 和键盘快捷键(在 Mac OS 中)

ios - 加载图像或使用 CGGradient 更有效?

ios - iPhone 6 截图大小

ios - 自定义分组 UITableView

swift - 使用 opencv : UIImage too slow 实时处理 ARkit 帧

swift - 使用 Finder 预览中的图标显示的文件的 NSCollectionView 网格