【发布时间】:2018-06-15 04:36:23
【问题描述】:
我在情节提要中有一个 textView 和 textfield。我想在 textfield 或 textView 显示键盘时向上移动滚动视图。对于 UITextField 来说,每件事都可以正常工作,但对于 textView 来说,那是行不通的。我有向上移动视图的代码:
func keyboardWillShow(notification: NSNotification) {
if let endFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
var keyboardHeight = view.bounds.height - endFrame.origin.y
if #available(iOS 11, *) {
if keyboardHeight > 0 {
keyboardHeight = keyboardHeight - view.safeAreaInsets.bottom
var contentInset:UIEdgeInsets = self.scrollView.contentInset
contentInset.bottom = keyboardHeight
viewBottom.constant = keyboardHeight
self.scrollView.contentInset = contentInset
}
}
else {
let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
var contentInset:UIEdgeInsets = self.scrollView.contentInset
contentInset.bottom = (keyboardSize.height)
self.scrollView.contentInset = contentInset
}
view.layoutIfNeeded()
}
}
func keyboardWillHide(notification: NSNotification) {
var contentInset:UIEdgeInsets = self.scrollView.contentInset
contentInset.bottom = 0
self.scrollView.contentInset = contentInset
}
用于显示键盘的 textfield 和 textView 有什么区别? 当我点击 textview 时调用的方法和滚动视图内容插入与点击文本字段但视图不向上移动时相同。 如何解决这个问题?
【问题讨论】:
-
你弄明白了吗?
-
@Do2 我无法管理它。现在我正在使用 IQKeyboardManagerSwift pod。
-
检查我的答案...
标签: ios swift keyboard textview uitextfield