[英]Scroll view when keyboard appears for UITextView, but not for the UITextField that's on the same view
I am using this code to scroll the view up when I start editing a UITextView
. 当我开始编辑
UITextView
时,我正在使用此代码向上滚动视图。
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(Login.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(Login.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
}
}
}
However, I also have a UITextField
at the top of this view, and the view also scrolls when I begin editing it. 但是,我在该视图的顶部也有一个
UITextField
,当我开始对其进行编辑时,该视图也会滚动。 I do not want this to happen. 我不希望这种情况发生。 How can I change my code to only scroll when the
UITextView
keyboard is active and not when the UITextField
keyboard is activated? 如何将我的代码更改为仅在激活
UITextView
键盘时滚动而不在激活UITextField
键盘时滚动?
Try this piece of code, user textview delgate 尝试这段代码,用户使用textview delgate
// scrollBy - pass the height you want your scrollview to be scrolled when keyboard appears // scrollBy-传递您希望在键盘出现时滚动滚动视图的高度
func textViewDidBeginEditing(_ textView: UITextView)
{
scrView.setContentOffset(CGPoint.init(x: 0, y: scrollBy), animated: true)
}
func textViewDidEndEditing(_ textView: UITextView)
{
scrView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)
self.view.endEditing(true);
}
我建议您应该在项目中添加TPkeyboard File而不是编码,这会在需要时切换键盘
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.