菜鸟教程小白 发表于 2022-12-11 19:17:26

ios - 键盘出现并查看滚动到顶部


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

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

<p>这是我的问题的一个小演示:</p>

<p> <a href="/image/T98jH.gif" rel="noreferrer noopener nofollow"><img src="/image/T98jH.gif" alt="enter image description here"/></a> </p>

<p>我做了一些研究并尝试了这个:</p>

<pre><code>   @objc func keyboardWillShow(_ notification: NSNotification) {
      print(&#34;show&#34;)
      if let keyboardSize = (notification.userInfo? as? NSValue)?.cgRectValue {
         if self.view.frame.origin.y == 0{
            self.view.frame.origin.y -= keyboardSize.height
         }
      }
   }

   @objc func keyboardWillHide(_ notification: NSNotification) {
      print(&#34;hide&#34;)
      if let keyboardSize = (notification.userInfo? as? NSValue)?.cgRectValue {
         if self.view.frame.origin.y != 0{
            self.view.frame.origin.y += keyboardSize.height
         }
      }
   }
</code></pre>

<p>它不起作用。这真的很奇怪,因为这是我遇到此问题的唯一 View 。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果您有对 textView 的引用,请尝试在您的 keyboardWillShow 函数中添加此代码。</p>

<pre><code>scrollView.scrollRectToVisible(textView.frame, animated: true)
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 键盘出现并查看滚动到顶部,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47282100/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47282100/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 键盘出现并查看滚动到顶部