ios UIScrollView 不滚动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19771205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIScrollView Not Scrolling?
提问by 67cherries
I have a UIScrollView
created in interface builder with a bunch of UITextView
and UIImageView
objects already added. I have it connected to an IBOutlet
, and under -(void)viewDidLoad
I set its content size to 1200x2000
. User interaction Enabled, Multitouch, Scrolling Enabled and shows vertical and horizontal scroll indicators are all set to YES
. When I launch the app in the iOS simulator, it doesn't scroll. What could possibly be the cause of this behavior?
我UIScrollView
在界面构建器中创建了一个已经添加了一堆UITextView
和UIImageView
对象的对象。我将它连接到IBOutlet
,然后-(void)viewDidLoad
将其内容大小设置为1200x2000
。用户交互启用、多点触控、滚动启用和显示垂直和水平滚动指示器都设置为YES
。当我在 iOS 模拟器中启动应用程序时,它不会滚动。这种行为的原因可能是什么?
回答by Nenad M
viewDidLoad is to soon. You must wait until after the layout of the views has taken place. Try setting the contentSize in viewDidAppear.
Are you using autolayout? If so, check out the following answer (and forget 1.): UIScrollView doesn't use autolayout constraints.
viewDidLoad 很快。您必须等到视图布局完成之后。尝试在 viewDidAppear 中设置 contentSize。
你在使用自动布局吗?如果是这样,请查看以下答案(并忘记 1。): UIScrollView 不使用自动布局约束。
回答by Randel S
The best way I know how to overcome this problem without resorting to writing code is that it all can be done within a storyboard and here is how:
我知道如何在不求助于编写代码的情况下克服这个问题的最好方法是,这一切都可以在故事板中完成,方法如下:
- Leave autolayout enabled.
- Make a generic UIView a subview of the UIScrollView. 2.1)Put all your View components inside this generic UIView.
- Make a autolayout constraint between a subview and the UIScrollView. You must pin the right and/or bottom of the last subview to the right and/or bottom of the UIScrollView. This is how the UIScrollView knows the content size.
- 启用自动布局。
- 使通用 UIView 成为 UIScrollView 的子视图。2.1) 将所有的 View 组件放在这个通用的 UIView 中。
- 在子视图和 UIScrollView 之间创建自动布局约束。您必须将最后一个子视图的右侧和/或底部固定到 UIScrollView 的右侧和/或底部。这就是 UIScrollView 知道内容大小的方式。
回答by Cezar
viewDidLoad
is indeed too soon, but viewDidAppear:animated
might not be late enough either. The ideal place to set the contentOffset
is inside viewDidLayoutSubviews
.
viewDidLoad
确实为时过早,但viewDidAppear:animated
也可能还不够晚。设置 的理想位置contentOffset
是在里面viewDidLayoutSubviews
。
回答by nithinreddy
If you're using Autolayout, make sure you put the code to set Content size within
如果您使用的是自动布局,请确保将代码设置为将内容大小设置在
viewDidLayoutSubviews
回答by AechoLiu
I use a custom view on the UIScrollView, for pull to refresh function, and the scroll view is not scrollable. I try set contentSize
with AutoLayout
, and try set it by codes too. The scroll view still cannot scroll.
我在 UIScrollView 上使用自定义视图,用于下拉刷新功能,并且滚动视图不可滚动。我尝试设置contentSize
与AutoLayout
,并通过代码也尝试设置。滚动视图仍然无法滚动。
After spending 3 ~ 4 hours, I solve it by contentInset
. It will tell scroll view to add some extra space for scrolling.
花了3~4小时后,我解决了contentInset
。它会告诉滚动视图为滚动添加一些额外的空间。
override func viewDidLayoutSubviews() {
// Add extra 15pt on the `top` of scrollView.
//self.scrollView.contentInset = UIEdgeInsetsMake(15, 0, 0, 0)
// Add extra 2pt on the `bottom` of scrollView, let it be scrollable. And the UI is more beautiful in my case.
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 2, 0)
}
Reference docs:
参考文档:
回答by Eden
This is a good walk-through of scroll-views in case anyone forgot after not using them for a while: https://medium.com/@pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7.
这是滚动视图的一个很好的演练,以防有人在一段时间不使用它们后忘记了:https: //medium.com/@pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout- in-interface-builder-218dcb4022d7。
Basically, make sure you have view -> scrollview -> view, like so:
then,
1. Set scroll view constraint (top, bottom, leading and trailing) to (0,0,0,0).
2. Set inner view constraint (top, bottom, leading and trailing) to (0,0,0,0).
3. Set inner view to have equal width and equal height with parent view.
4. Select height constraint of inner view and set priority to low (250).
基本上,确保您有视图 ->
滚动视图-> 视图,如下所示:
然后,1. 将滚动视图约束(顶部、底部、前导和尾随)设置为 (0,0,0,0)。
2. 将内部视图约束(顶部、底部、前导和尾随)设置为 (0,0,0,0)。
3. 设置内部视图与父视图等宽和等高。
4. 选择内部视图的高度约束并将优先级设置为低(250)。