【发布时间】:2017-08-11 11:10:08
【问题描述】:
问题陈述:我有 UIView 的 Nib 文件包含 UIScrollview,在滚动视图中我有几个 TextFields 和底部的 TextView。我想要的是在 textfield 或 Textview 开始编辑时向上滚动。
我尝试了什么:
在客户方法
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
我正在调用这个方法父视图。
通知处理:
func keyboardWasShown(notification: NSNotification)
{
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
keyboardFrame = self.convert(keyboardFrame, from: nil)
var contentInset:UIEdgeInsets = self.mainScroll.contentInset
contentInset.bottom = keyboardFrame.size.height
self.mainScroll.contentInset = contentInset
}
这对 UITextFields 非常有效,但不适用于 UITextView。知道错误在哪里。
PS:我也设置了 UITextField 和 UITextView 的 Delegates。
任何帮助将不胜感激。
【问题讨论】:
-
你可以使用 contentoffset 来滚动滚动视图。
-
使用github.com/hackiftekhar/IQKeyboardManager,它会自动检测 textField 或 textView 并相应地管理所有键盘处理。
-
@ZahidShabbir : 在 textview 的情况下,scrollview 没有向上滚动或滚动一点?
-
@zahidshabbir : 你试过使用 contentOffset 吗?
-
@zahidshabbir 请看下面的答案。
标签: ios ipad uiscrollview keyboard uitextview