且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无论大型contentSize如何,UIScrollView都不会滚动

更新时间:2023-11-18 08:26:46

关闭自动布局,但不是解决方案。如果您确实需要自动布局,请使用它,如果您不需要,请将其关闭。但这不是此解决方案的正确修复。

Turning Auto Layout off works, but that's not the solution. If you really need Auto Layout, then use it, if you don't need it, turn it off. But that is not the correct fix for this solution.

UIScrollView 与自动布局中的其他视图的工作方式不同。这是关于Auto的 Apple的发布说明布局,我复制了有趣的一点(强调我的,在第三个项目点):

UIScrollView works differently with other views in Auto Layout. Here is Apple's release note on Auto Layout, I've copied the interesting bit (emphasis mine, at third bullet point):


以下是关于Auto Layout支持的一些注意事项UIScrollView:

Here are some notes regarding Auto Layout support for UIScrollView:


  • 通常,自动布局会将视图的顶部,左侧,底部和右侧边缘视为可见边缘。也就是说,如果你将视图固定在超级视图的左边缘
    ,那么你真的将它固定到超级视图边界的
    最小x值​​。更改边界原点
    的superview不会更改视图的位置。

  • UIScrollView类通过更改其边界的原点来滚动其内容。为了使其适用于自动布局,滚动视图中的顶部,左侧,底部,
    和右边缘现在表示其内容
    视图的边缘。

  • 滚动视图子视图的约束必须导致填充大小,然后将其解释为
    滚动视图的内容大小。 (这不应与用于自动布局的
    intrinsicContentSize方法相混淆。)要使用自动布局调整滚动
    视图框架的大小,约束必须是显式的
    ,关于宽度和高度。滚动视图,或
    滚动视图的边缘必须绑定到其子树之外的视图。

  • 请注意,您可以创建滚动视图的子视图通过在视图和滚动视图的子树外部的视图之间创建约束
    ,似乎浮动(不滚动)其他滚动内容,例如
    滚动视图的超级视图。

  • In general, Auto Layout considers the top, left, bottom, and right edges of a view to be the visible edges. That is, if you pin a view to the left edge of its superview, you’re really pinning it to the minimum x-value of the superview’s bounds. Changing the bounds origin of the superview does not change the position of the view.
  • The UIScrollView class scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the top, left, bottom, and right edges within a scroll view now mean the edges of its content view.
  • The constraints on the subviews of the scroll view must result in a size to fill, which is then interpreted as the content size of the scroll view. (This should not be confused with the intrinsicContentSize method used for Auto Layout.) To size the scroll view’s frame with Auto Layout, constraints must either be explicit regarding the width and height of the scroll view, or the edges of the scroll view must be tied to views outside of its subtree.
  • Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.

Apple继续展示如何正确使用 UIScrollView 的示例自动布局。

Apple then goes on to show example of how to correctly use UIScrollView with Auto Layout.

作为一般规则,最简单的解决方法之一是在元素与UIScrollView底部之间创建约束。因此,在您希望位于UIScrollView底部的元素中,创建此底部空间约束:

As a general rule, one of the easiest fix is to create a constraint between the element to the bottom of the UIScrollView. So in the element that you want to be at the bottom of the UIScrollView, create this bottom space constraint:

再次,如果您不想使用自动布局,请将其关闭。然后,您可以通常的方式设置 contentSize 。但你应该理解的是,这是自动布局的预期行为。

Once again, if you do not want to use Auto Layout, then turn it off. You can then set the contentSize the usual way. But what you should understand is that this is an intended behaviour of Auto Layout.