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

ios - scrollView 的 ContentInset


                                            <p><p>这是我的 View ,左边是键盘不显示时的 View ,右边是键盘显示时我想要实现的 View 。</p>

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

<p>我的所有字段都添加到一个 ScrollView 中,然后我设置这样的约束,field1 topConstraint 设置为 scrollView 顶部,field4 bottomConstraint 设置为 scrollView 底部。所有其他字段都附在下面的字段中。 </p>

<p>从最右边的模型中可以看出,当显示键盘时,我想像这样重新调整 View 。所以我现在要做的是:</p>

<pre><code>func keyboardWillShow(notification: NSNotification) {
       self.field2.snp_updateConstraints { make -&gt; Void in
          make.bottom.equalTo(self.field3.snp_top).offset(8)
       }
</code></pre>

<p>用这个我调整了 field2 和 field3 之间的大空间,到目前为止一切都很好。然后我继续设置 contentInset:</p>

<pre><code>    let contentInset = UIEdgeInsetsMake(0, 0,getKeyboardSize(notification).height, 0)

    self.scrollView.contentInset = contentInset
    self.scrollView.scrollIndicatorInsets = contentInset
</code></pre>

<p>但是 scrollView 没有按我想要的方式调整,因为 field3 仍然隐藏在键盘后面的一半。 </p>

<p>发表这篇文章的原因是我显然不明白 contentInsets 是如何与 scrollView 一起工作的,如果可能的话,我想避免手动设置 contentOffset。 </p>

<p>我设置的约束似乎有问题。 field4 的当前约束是我将它设置为绑定(bind)到 View 而不是 ScrollView 底部,并且我将 ScrollView 的每个边缘设置为绑定(bind)到 View 。 </p>

<p><strong>更新:</strong>
如果我将field4底部约束设置为绑定(bind)到scrollView,scrollViews高度为0,如果我之前将scrollView设置为与 View 本身一样大,它怎么会不知道它应该是什么大小?这真的让我很困惑..</p>

<p><strong>更新 2</strong>
我正在设置这样的 ScrollView 约束:</p>

<pre><code>    self.scrollView.snp_makeConstraints { make -&gt; Void in
      make.top.equalTo(self.snp_topLayoutGuideBottom)
      make.left.right.equalTo(self.view)
      make.bottom.equalTo(self.snp_bottomLayoutGuideBottom)
    }
</code></pre>

<p>从UIDebugger我可以看到scrollView的高度是一样的,但是Y坐标变成了20,但是还是不能滚动进去??此外,在调试时,我可以将 scrollViews contentSize 设置为大于框架,这意味着它应该是可滚动的,但事实并非如此。</p>

<p><strong>更新 3</strong>
我现在明白 Field1 和 Field4 都需要附加到 ScrollView 的顶部和底部。因此,scrollViews frame 和 scrollViews contentSize 之间存在差异,因为当 contentSize > frame scrollView 可滚动时,它的工作方式与预期相同。
所以这对我来说是向前迈出的一大步,现在唯一奇怪的是 contentInset。 </p>

<p>我对 contentInset 的理解是,您可以指定 View 在任一方向上应缩小多少。所以现在我正在使用scrollView底部的键盘高度进行插入,但它没有滚动,但是当我将顶部contentInset设置为一些随机负数(如-100)时,它是滚动的,为什么调整底部插图时未实现此行为? </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>所以我用以下方法解决了这个问题:</p>

<p>我确保将 Field1 和 Field4 约束设置为 ScrollView ,这样 ScrollView 就能够自动计算高度,并且如果某些内容在 View 之外,也可以滚动。然后我从键盘顶部和框架计算可见框架。 </p>

<pre><code>   func keyboardWillShow(notification: NSNotification) {

    self.Field2.snp_updateConstraints { make -&gt; Void in
         make.bottom.equalTo(self.Field3.snp_top)
    }

    self.Field3.snp_updateConstraints { make -&gt; Void in
         make.bottom.equalTo(self.Field4.snp_top)
    }

    self.view.layoutIfNeeded()

    let contentInset = UIEdgeInsetsMake(0, 0,getKeyboardSize(notification).height + 4, 0)
    let visibleRect = UIEdgeInsetsInsetRect(self.scrollView.frame, contentInset)
    let scrollToOffset = max(0,self.Field4.frame.maxY - visibleRect.height)
    self.scrollView.setContentOffset(CGPoint(x: 0, y: scrollToOffset), animated: true)
}
</code></pre>

<p>关键是在更改约束之后,我需要使当前布局无效并再次布局 View ,这样我们计算可以正确完成更新约束的位置。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - scrollView 的 ContentInset,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38832770/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38832770/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - scrollView 的 ContentInset