Want ability to manually scroll to pull textfield above keyboard
问题显然是当用户看到键盘阻止他们想要输入的文本字段,而不是点击返回,或者关闭键盘并点击下一个字段时,他们会尝试平移到下一个字段。 由于我的内容与iPad的大小相匹配,因此当用户尝试平移时,滚动视图不会自动滚动。 老实说,除非键盘在屏幕上,否则我不希望它滚动。
但是,在滚动视图上启用滚动并不能解决问题; 即使在这种情况下,它仍然不会响应平移。 也没有使viewcontroller成为scrollview的委托并覆盖函数scrollViewDidScroll。 如何启用滚动视图以启用平移,尤其是仅在启用键盘时?
由于已经发布了一个不太有用的解决方案,我想我会发布我的keyboardWillBeShown和keyboardWillBeHidden代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | func keyboardWillBeShown(_ sender: Notification) { self.myScrollView.isScrollEnabled = true let info: NSDictionary = (sender as NSNotification).userInfo! as NSDictionary let value: NSValue = info.value(forKey: UIKeyboardFrameBeginUserInfoKey) as! NSValue let keyboardSize: CGSize = value.cgRectValue.size let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0) self.myScrollView.contentInset = contentInsets self.myScrollView.scrollIndicatorInsets = contentInsets self.myScrollView.scrollRectToVisible(self.myScrollView.frame, animated: true) var aRect: CGRect = self.view.frame aRect.size.height -= keyboardSize.height let activeTextFieldRect: CGRect? = activeField?.frame let activeTextFieldOrigin: CGPoint? = activeTextFieldRect?.origin if activeTextFieldOrigin != nil { if (!aRect.contains(activeTextFieldOrigin!)) { let scrollpoint : CGPoint = CGPoint(x: 0.0, y: activeField!.frame.origin.y - keyboardSize.height) self.myScrollView.setContentOffset(scrollpoint, animated: true)//.scrollRectToVisible((activeField?.frame)!, animated: true) } } } func keyboardWillBeHidden(_ sender: Notification) { myScrollView.isScrollEnabled = false let contentInsets: UIEdgeInsets = UIEdgeInsets.zero myScrollView.contentInset = contentInsets myScrollView.scrollIndicatorInsets = contentInsets } |
我想出了如何得到我想要的东西。 将键盘显示时,将myScrollView.contentSize设置为与屏幕和键盘一样大,并将大小缩小到隐藏键盘时的大小。 然后使视图控制器成为UIScrollViewDelegate,设置myScrollView.delegate = self,并实现scrollViewDidScroll,这样如果文本字段正在编辑,并且该文本字段不是空白,则更改哪个文本字段是第一个响应者。 一旦你意识到设置.contentSize的技巧,一块蛋糕!
尝试这个,
声明要使用的变量
1 2 | var originalViewCGRect: CGRect? var originalOffset: CGPoint! |
在
1 2 3 4 5 6 7 8 | NotificationCenter.default.addObserver(self , selector: #selector(keyboardWillAppear(_:)) , name: NSNotification.Name.UIKeyboardWillShow , object: nil) NotificationCenter.default.addObserver(self , selector: #selector(keyboardWillDisappear(_:)) , name: NSNotification.Name.UIKeyboardWillHide , object: nil) |
最后,添加这些功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | func keyboardWillAppear(_ notification: Foundation.Notification){ self.scrollView.scrollRectToVisible(self.scrollView.frame, animated: true) let keyboardSize:CGSize = ((notification as NSNotification).userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size let insets = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0) self.scrollView.contentInset = insets } func keyboardWillDisappear(_ notification: Foundation.Notification){ let beginFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue let delta = (endFrame.origin.y - beginFrame.origin.y) self.view.frame = self.originalViewCGRect! self.scrollView.setContentOffset(CGPoint(x: 0, y: max(self.scrollView.contentOffset.y - delta, 0)), animated: true) } |
总之,当键盘被显示和/或隐藏时,这将调整滚动视图的内容插入。
希望这可以帮助。
编辑
修正了调整