无论大型contentSize如何,UIScrollView都不会滚动 [英] UIScrollView not scrolling regardless of large contentSize

查看:282
本文介绍了无论大型contentSize如何,UIScrollView都不会滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Interface Builder中创建了一个UIScrollView并添加了子视图,但是我无法滚动它。我看过其他类似的问题,但我已经适当地设置了我的contentSize。事实上,我甚至尝试过设置令人难以置信的大型内容,但它无法发挥作用。我错过了什么?注意,UIScrollView中是一个IBOutlet:

I have created a UIScrollView and added subviews to it in Interface Builder, but I am not able to get it to scroll. I have looked at other similar questions, but I have set my contentSize appropriately. In fact, I have even tried setting incredibly large contentSize and it fails to make a difference. What am I missing? Note that the UIScrollView is an IBOutlet:

@property (strong, nonatomic) IBOutlet UIScrollView *scroll;

这是在我的viewDidLoad中:

This is in my viewDidLoad:

self.scroll.scrollEnabled = YES;
[self.scroll setContentSize: CGSizeMake(2400,8000)];

以下是Interface Builder的截图:

Here are screenshots from Interface Builder:

这是运行时的视图,但任何水平或垂直滚动​​的尝试在任何时候都不起作用。

Here is the view while running, but any attempt at horizontal or vertical scrolling at any point does not work.

推荐答案

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

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.

这篇关于无论大型contentSize如何,UIScrollView都不会滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆