简体   繁体   English

iOS 8-UIScrollView无法滚动

[英]iOS 8 - UIScrollView doesn't scroll

I want to make vertical scrolling depends on content inside Content View . 我想使垂直滚动取决于Content View中的内容 I've created following hierarchy: View -> Scroll View -> Content View . 我创建了以下层次结构: 视图 -> 滚动视图 -> 内容视图

  • I pinned (top, bottom, left, right) Scroll View to the Main View 我固定(顶,底,左,右) 滚动视图主视图
  • I also pinned Content View to the Scroll View 我还将内容视图固定到滚动视图
  • 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 然后我在内容视图主视图之间添加了相同宽度相同高度约束,以便滚动视图可以计算其内容大小

After all these actions, Scroll View doesn't scroll at all (it just shrinks the content). 完成所有这些操作后,“ 滚动视图”根本不滚动(它只是缩小内容)。 It scrolls only if i add scrollView.contentSize.height to the viewWillLayoutSubviews() . 仅当我将scrollView.contentSize.height添加到viewWillLayoutSubviews()时,它才会滚动。 However i don't want to hardcode this height value. 但是我不想硬编码这个高度值。 I thought Autolayout should automatically calculate content height and add appropriate scrolling. 我认为自动版式应该自动计算内容高度并添加适当的滚动。 Does anyone know the proper way to solve this problem? 有谁知道解决这个问题的正确方法?

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. 它非常适合编辑,但是当您运行应用程序时,视图的框架大小根据iPhone或iPad而有所不同,但是滚动视图的框架大小不会改变。 It's still the same size that you set in the storyboard. 它的大小仍与您在情节提要中设置的大小相同。

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;

• 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. 将子视图放在scrollViewscrollView其添加顶部,底部,前导和尾随约束应足够。

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 创建相等的高度约束(和相等的宽度)时,阻止了内容调整为其固有大小的大小,该大小由自动布局用于计算scrollView.contentSize

You can keep the equal widths constraints if you want your scrollView to only scroll vertically. 如果希望scrollView仅垂直滚动,则可以保持相等的宽度约束。

内容视图的框架应大于滚动视图,然后设置contentSize以确保滚动范围。

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.contentSize!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM