xcode iOS 8 - UIScrollView 不滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31758589/
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
iOS 8 - UIScrollView doesn't scroll
提问by Dmitrue
I want to make vertical scrolling depends on content inside Content View. I've created following hierarchy: View-> Scroll View-> Content View.
我想让垂直滚动取决于Content View 中的内容。我创建了以下层次结构:View-> Scroll View-> Content View。
- I pinned (top, bottom, left, right) Scroll Viewto the Main View
- I also pinned Content Viewto the Scroll View
- Then i added Same Widthand Same Heightconstraints between Content Viewand Main Viewin order to Scroll Viewcould calculate it's content size
- 我固定(顶部,底部,左侧,右侧)滚动视图到主视图
- 我还将内容视图固定到滚动视图
- 然后我在内容视图和主视图之间添加了相同的宽度和相同的高度约束,以便滚动视图可以计算它的内容大小
After all these actions, Scroll Viewdoesn't scroll at all (it just shrinks the content). It scrolls only if i add scrollView.contentSize.heightto the viewWillLayoutSubviews(). However i don't want to hardcode this height value. I thought Autolayoutshould automatically calculate content height and add appropriate scrolling. Does anyone know the proper way to solve this problem?
在所有这些操作之后,滚动视图根本不滚动(它只是缩小了内容)。只有当我将scrollView.contentSize.height添加到viewWillLayoutSubviews() 时它才会滚动。但是我不想硬编码这个高度值。我认为Autolayout应该自动计算内容高度并添加适当的滚动。有谁知道解决这个问题的正确方法?
回答by brandav
I had the same issue. I fixed it by deleting the scroll view's constraints and then setting the scroll view's frame to equal the view's frame in viewDidAppear:
我遇到过同样的问题。我通过删除滚动视图的约束然后将滚动视图的框架设置为等于 viewDidAppear 中的视图框架来修复它:
self.scrollView.frame = self.view.frame;
The issue occurs when you set the scroll view's width and height in storyboard to equal the view so you can see the full content. It's perfect for editing, but when you run your app, the view's frame size is different based on the iPhone or iPad, but the scroll view's frame size doesn't change. It's still the same size that you set in the storyboard.
当您在故事板中将滚动视图的宽度和高度设置为等于视图以便您可以看到完整内容时,就会出现问题。它非常适合编辑,但是当您运行您的应用程序时,视图的框架大小因 iPhone 或 iPad 而异,但滚动视图的框架大小不会改变。它仍然与您在故事板中设置的大小相同。
That's why the scroll view will only work when you hard code the content size to a randomly large value. That value has to be greater than the current scroll view's frame size in order for it to work. Once you set the scroll view's frame to equal the view's frame, the content size setting will work properly:
这就是为什么滚动视图仅在您将内容大小硬编码为随机大值时才起作用的原因。该值必须大于当前滚动视图的框架大小才能使其工作。一旦您将滚动视图的框架设置为等于视图的框架,内容大小设置将正常工作:
self.scrollView.contentSize = self.contentView.frame.size;
回答by Yariv Nissim
? Then i added Same Width and Same Height constraints between Content View and Main View in order to Scroll View could calculate it's content size
? 然后我在内容视图和主视图之间添加了相同的宽度和相同的高度约束,以便滚动视图可以计算它的内容大小
Remove this part. Putting subviews within the scrollView
and adding top, bottom, leading and trailing constraints to it should be enough.
When you created the equal heights constraint (and equal widths), you prevented your content from resizing to its intrinsic size, which is used by auto layout to calculate the scrollView.contentSize
You can keep the equal widths constraints if you want your scrollView
to only scroll vertically.
移除这部分。将子视图放入其中scrollView
并为其添加顶部、底部、前导和尾随约束就足够了。
当你创建等高约束(和等宽)时,你阻止了你的内容调整到它的固有大小,自动布局使用它来计算scrollView.contentSize
如果你只想scrollView
垂直滚动,你可以保持等宽约束。
回答by Lucifron
The frame of content view should be larger than scrollview then set the contentSize to ensure the range of scrolling.
content view的frame要大于scrollview,然后设置contentSize来保证滚动的范围。
回答by NSGangster
Why do you need a content view inside of your scrollView? It sounds like your Content View is whats re-sizing your content and your scroll view's content size is being set to your Content View which isn't being dynamically changed. Try laying out your content in the scroll view itself so your scroll views content size will actually change depending on what's inside. Make sure your updating scrollView.contentSize!
为什么你的 scrollView 需要一个内容视图?听起来您的内容视图正在重新调整您的内容大小,并且您的滚动视图的内容大小正在设置为您的内容视图,而该内容视图并未动态更改。尝试在滚动视图本身中布置您的内容,以便您的滚动视图内容大小实际上会根据里面的内容而改变。确保您正在更新 scrollView.contentSize!