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

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

问题描述

我在 Interface Builder 中创建了一个 UIScrollView 并为其添加了子视图,但我无法让它滚动.我看过其他类似的问题,但我已经适当地设置了我的 contentSize.事实上,我什至尝试过设置非常大的 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;

这是在我看来DidLoad:

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.

推荐答案

关闭自动布局可以,但这不是解决方案.如果你真的需要Auto Layout,那就使用它,如果你不需要它,把它关掉.但这不是此解决方案的正确解决方案.

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 与自动布局中的其他视图不同.这是 Apple 的关于 Auto 的发行说明布局,我复制了有趣的部分(强调我的,在第三个要点):

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):

以下是有关 UIScrollView 的自动布局支持的一些说明:

Here are some notes regarding Auto Layout support for UIScrollView:

  • 通常,自动布局将视图的顶部、左侧、底部和右侧边缘视为可见边缘.也就是说,如果您将视图固定到它的超级视图的左边缘,你真的把它固定在超级视图边界的最小 x 值.更改边界原点父视图的位置不会改变视图的位置.
  • UIScrollView 类通过更改其边界的原点来滚动其内容.为了使这项工作与自动布局一起使用,顶部、左侧、底部、滚动视图中的右边缘现在意味着其内容的边缘查看.
  • 对滚动视图的子视图的约束必须导致要填充的大小,然后将其解释为滚动视图的内容大小滚动视图.(这不应与用于自动布局的 intrinsicContentSize 方法.)调整滚动的大小具有自动布局的视图框架,约束必须是显式的关于滚动视图的宽度和高度,或滚动视图必须绑定到其子树之外的视图.
  • 请注意,您可以通过创建约束使滚动视图的子视图看起来浮动(不滚动)在其他滚动内容之上在视图和滚动视图的子树之外的视图之间,例如滚动视图的超级视图.

Apple 然后继续展示如何正确使用 UIScrollView 和 Auto Layout 的示例.

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.但你应该明白,这是 Auto Layout 的预期行为.

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.

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

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