无法让 UIScrollView 工作 [英] Can't get UIScrollView to work

查看:23
本文介绍了无法让 UIScrollView 工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个故事板应用.默认情况下,我在视图控制器中的 UIView 顶部添加了一个 UIScrollView.然后我在滚动视图上添加了一些 UI 元素.在设计了 Scroll 视图的可视部分之后,我使用它的手柄垂直拉伸它并将它向上推一点,这样我就可以看到该部分来设计屏幕的其余部分.完成后,我将滚动视图定位回以适应视图.请注意,我没有调整滚动视图的大小以匹配来自 IB 的视图的大小.

然后我将所有 4 个约束添加到滚动视图(前导和尾随空间到超级视图,顶部和底部空间到超级视图).然后我选择了 UIView,打开尺寸检查器并将底部空间更改为 superview 的值为 0.这会自动调整 UIScrollView 的大小以适应视图控制器.>

然后我在 viewDidLoad 方法中添加了以下代码行.

self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);self.scrollView.frame = self.view.frame;

但这行不通.UIScrollView 不会滚动.是的,滚动已启用.

谁能告诉我我在这里可能遗漏了什么?

谢谢.

我在此处找到了我的一个旧项目.它利用了我上面刚刚解释的方式,并且有效!不知道为什么它不再.

我还在此问题中添加了一个演示项目.

解决方案

如果我正确理解您的代码(视图的屏幕截图会有所帮助),您将滚动视图框架设置为视图的全尺寸,然后将 contentSize 设置为视图的大小.仅当 contentSize 大于其大小时,滚动视图才会滚动.

This is a storyboard app. I added a UIScrollView on top of the UIView that is there in the view controller by default. Then I added some UI elements on to the scroll view. After designing the viewable part of the Scroll view, using its handles I stretched it vertically and pushed it up a bit so that that part is visible for me to design the rest of the screen. After I was done I positioned the scroll view back to fit the view. Please note that I did not resize the scroll view to match the size of the view from the IB.

Then I added all 4 constraints to the scroll view (Leading and Trailing space to superview, Top and Bottom space to superview). Then I selected the UIView, opened up the size inspector and changed the Bottom Space to superview's value to 0. That automatically resized the UIScrollView to fit inside the view controller.

Then I added the following lines of code inside the viewDidLoad method.

self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);
self.scrollView.frame = self.view.frame;

But this does not work. The UIScrollView won't scroll. Yes, the scrolling is enabled.

Can anyone please tell me what I might be missing here?

Thank you.

EDIT:

I found an old project of mine here. It utilize the way I just explained above and it works! No idea why it doesn't anymore.

I also added a demo project here with the issue.

解决方案

If I understood your code correctly (a screenshot of the view would help) you set the scrollviews frame to be full size of the view and set then contentSize to the view's size. A scrollview scrolls only if the contentSize is larger than its size.

这篇关于无法让 UIScrollView 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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