使用 UIScrollView 和 setContentSize 时设置正确的大小
我只是想要拥有比普通 iPhone 屏幕所能容纳的更多的控件。以在“通讯录”应用中查看某人的联系信息为例。
看来我应该使用 UIScrollView 来实现这个。
假设我需要大约 800 像素。因此,在我的 NIB 中,我将 ScrollView 的视图设置为高度为 800。然后,我将控件添加到 Scroll View 下。
我建立连接,然后在 viewDidLoad 中执行通常的操作:
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 800)];
如果我在此状态下构建并运行,它不会滚动。所以在网上查了一下,人们说你必须将其设置为大于 UIScrollView。
我似乎无法找到有关如何实际计算 setContentSize 中的 800 的信息。难道只是猜测直到看起来正确为止吗?如果我将其设置为 801,我永远不会看到滚动视图底部的所有内容,如果我将其设置为 1000,我可以看到底部,但它并没有真正在底部结束。
我只是希望如果用户向下滚动,它会正确地结束在底部。
这是如何计算的?或者我错过了什么?这似乎是一个相当常见的用途,但尚未找到模拟这个简单示例的代码示例。
I simply just want to have more controls than I can fit on the normal iPhone screen. Take for example viewing someone's contact information in the Contacts app.
It appears that I should use a UIScrollView to implement this.
Lets say I need about 800 pixels for this. So in my NIB, I set the View of the ScrollView to have a height of 800. I then add my controls under the Scroll View.
I make my connection, and then in the viewDidLoad, I do the usual:
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 800)];
If I build and run at this state, it wont scroll. So looking around online, people say that you have to set this to be larger than the UIScrollView.
I can't seem to find information on how to actually calculate what that 800 should be in the setContentSize. Is it just guessing until it looks right? If I set it to 801, I never see everything at the bottom of my Scroll View, and if I set it to 1000 I can see the bottom, but it doesn't really end at the bottom.
I just want that if the user scrolls down, it ends at the bottom correctly.
How is this calculated? Or am I missing something? This seems like a rather common use but haven't found code examples that simulate this simple example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
滚动视图通过显示较小的区域来提供更大画布的视角,但允许您平移画布。现在,当您将滚动视图的框架大小和内容大小设置为相同时,目的就达不到了。因此没有滚动。
当您在大小为 320x480 的窗口/视图上将滚动视图的框架定义为 320x800 时,部分视图将被剪掉。它不可见,但它就在那里。你的内容也在那里。您需要做的就是设置滚动视图以匹配窗口的大小,即 320x480。现在您将可以滚动,因为视角小于画布。
Scrollview provides a perspective onto a bigger canvas by showing a smaller region but allowing you to pan across the canvas. Now when you set the scrollview's frame size and the content size to be same, the purpose is defeated. Hence no scrolling.
When you define the scroll view's frame to be 320x800 on a window/view that is of size 320x480, part of the view is clipped off. It is not visible but its there. You content is there too. What you need to do is to set the scroll view to match the window's size i.e. 320x480. Now you will have scrolling as the perspective is smaller than the canvas.