ios UIScrollView 不滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2824435/
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 Mark
I have a UIScrollView
which contains many UIImageView
s, UILabels, etc... the labels are much longer that the UIScrollView
, but when I run the app, I cannot click and scroll down...
我有一个UIScrollView
包含许多UIImageView
s、UILabels 等的...标签比 长得多UIScrollView
,但是当我运行该应用程序时,我无法单击并向下滚动...
Why might this be?
为什么会这样?
Thanks
谢谢
回答by Bogatyr
It's always good to show a complete working code snippet:
显示完整的工作代码片段总是好的:
// in viewDidLoad (if using Autolayout check note below):
UIScrollView *myScrollView;
UIView *contentView;
// scrollview won't scroll unless content size explicitly set
[myScrollView addSubview:contentView];//if the contentView is not already inside your scrollview in your xib/StoryBoard doc
myScrollView.contentSize = contentView.frame.size; //sets ScrollView content size
Swift 4.0
斯威夫特 4.0
let myScrollView
let contentView
// scrollview won't scroll unless content size explicitly set
myScrollView.addSubview(contentView)//if the contentView is not already inside your scrollview in your xib/StoryBoard doc
myScrollView.contentSize = contentView.frame.size //sets ScrollView content size
I have not found a way to set contentSize in IB (as of Xcode 5.0).
我还没有找到在 IB 中设置 contentSize 的方法(从 Xcode 5.0 开始)。
Note:
If you are using Autolayout the best place to put this code is inside the -(void)viewDidLayoutSubviews
method .
注意:如果您使用 Autolayout,最好将这段代码放在-(void)viewDidLayoutSubviews
method内部。
回答by user1539652
If you cannot scroll the view even after you set contentSizecorrectly, make sure you uncheck"Use AutoLayout" in Interface Builder -> File Inspector.
如果即使在正确设置contentSize后仍无法滚动视图,请确保在 Interface Builder -> File Inspector 中取消选中“Use AutoLayout”。
回答by Chris Vasselli
You need to set the contentSize
property of the scroll view in order for it to scroll properly.
您需要设置contentSize
滚动视图的属性才能使其正确滚动。
If you're using autolayout, you need to set contentSize
in viewDidLayoutSubviews
in order for it to be applied after the autolayout completes.
如果您使用自动布局,你需要设置contentSize
的viewDidLayoutSubviews
,以便它的自动布局完成后应用。
The code could look like this:
代码可能如下所示:
-(void)viewDidLayoutSubviews
{
// The scrollview needs to know the content size for it to work correctly
self.scrollView.contentSize = CGSizeMake(
self.scrollContent.frame.size.width,
self.scrollContent.frame.size.height + 300
);
}
回答by Jasper Blues
The answer above is correct - to make scrolling happen, it's necessary to set the content size.
上面的答案是正确的 - 要进行滚动,必须设置内容大小。
If you're using interface builder a neat way to do this is with user defined runtime attributes. Eg:
如果您正在使用界面构建器,一个巧妙的方法是使用用户定义的运行时属性。例如:
回答by Denis Kutlubaev
Try to resize the content size to huge numbers. I couldn't understand why my scroll view doesn't scroll even when its content size seems to be bigger than control size. I discovered that if the content size is smaller than needed, it doesn't work also.
尝试将内容大小调整为大量数字。我不明白为什么我的滚动视图即使在其内容大小似乎大于控件大小时也不滚动。我发现如果内容大小小于需要,它也不起作用。
self.scrollView.contentSize = CGSizeMake(2000, 2000);
Instead of 2000 you can put your own big numbers. And if it works, it means that your content size is not big enough when you resize.
您可以输入自己的大数字而不是 2000。如果它有效,则意味着您调整大小时您的内容大小不够大。
The delegate is not necessary for scroll view to work.
滚动视图工作不需要委托。
回答by Ben Gottlieb
Make sure you have the contentSize property of the scroll view set to the correct size (ie, one large enough to encompass all your content.)
确保将滚动视图的 contentSize 属性设置为正确的大小(即,足够大以包含所有内容。)
回答by Gaurav Kumkar
Uncheck 'Use Autolayout' did the trick for me.
取消选中“使用自动布局”对我有用。
Environment: xCode 5.0.2 Storyboards ios7
环境:xCode 5.0.2 Storyboards ios7
回答by devios1
In my case I had to set delaysContentTouches
to true because the objects inside the scrollView were all capturing the touch events and handling themselves rather than letting the scrollView itself handle it.
就我而言,我必须设置delaysContentTouches
为 true,因为 scrollView 内的对象都在捕获触摸事件并自行处理,而不是让 scrollView 本身处理它。
回答by Vinayak Parmar
Set contentSize
property of UIScrollview
in ViewDidLayoutSubviews
method. Something like this
设置in方法的contentSize
属性。像这样的东西UIScrollview
ViewDidLayoutSubviews
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.contentSize = CGSizeMake(view.frame.size.width, view.frame.size.height)
}
回答by Jason G
if you are getting a message (IOS8 / swift) that viewDidLayoutSubviews
does not exist, use the following instead
如果您收到一条viewDidLayoutSubviews
不存在的消息(IOS8 / swift),请改用以下内容
override func viewDidAppear(animated: Bool)
This fixed it for me
这为我修好了