ios - 当键盘存在时,使用 UITextView 向上移动 UIAlertController 样式警报
<p><p>我有一个带有 UITextView 的 AlertController。</p>
<p>当 UIExView 成为第一响应者时,alter 不会随着键盘向上移动。</p>
<p>这是我的代码:</p>
<pre><code>@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()
})
}
</code></pre>
<p>这是我的结果:</p>
<p> <a href="/image/kCUcZ.png" rel="noreferrer noopener nofollow"><img src="/image/kCUcZ.png" alt="enter image description here"/></a>
有解决办法吗?</p>
<p>提前致谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>只需添加TextField,然后将其删除</p>
<pre><code> alert.addTextField { field in
field.translatesAutoresizingMaskIntoConstraints = false
field.heightAnchor.constraint(equalToConstant: 0).isActive = true
}
let inCntrlr =alert.childViewControllers.view!
inCntrlr.removeFromSuperview()
</code></pre>
<p>然后您可以添加自己的 View 。这里有一个
<a href="/image/hkHPh.png" rel="noreferrer noopener nofollow">result</a> </p></p>
<p style="font-size: 20px;">关于ios - 当键盘存在时,使用 UITextView 向上移动 UIAlertController 样式警报,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/41896083/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/41896083/
</a>
</p>
页:
[1]