OGeek|极客世界-中国程序员成长平台

标题: ios - 键盘出现并查看滚动到顶部 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:17
标题: ios - 键盘出现并查看滚动到顶部

我的代码有问题:当我点击 UITextView 并显示键盘时,会滚动到顶部(我真的不知道为什么!) 我必须自己向下滚动才能返回 UITextView。

我在 tableView 中,我的单元格具有动态高度。

这是我的问题的一个小演示:

enter image description here

我做了一些研究并尝试了这个:

   @objc func keyboardWillShow(_ notification: NSNotification) {
      print("show")
      if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
         if self.view.frame.origin.y == 0{
            self.view.frame.origin.y -= keyboardSize.height
         }
      }
   }

   @objc func keyboardWillHide(_ notification: NSNotification) {
      print("hide")
      if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
         if self.view.frame.origin.y != 0{
            self.view.frame.origin.y += keyboardSize.height
         }
      }
   }

它不起作用。这真的很奇怪,因为这是我遇到此问题的唯一 View 。



Best Answer-推荐答案


如果您有对 textView 的引用,请尝试在您的 keyboardWillShow 函数中添加此代码。

scrollView.scrollRectToVisible(textView.frame, animated: true)

关于ios - 键盘出现并查看滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47282100/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4