使用UIScrollView实现垂直滚动的正确方法



我知道我几分钟前问了一个问题,但我想在寻求社区帮助之前澄清几点。我是iOS开发新手,即将完成我的第一款应用。唯一阻碍我的是UIScrollView。我实在不明白,需要你的帮助。

这是一个带有选项卡栏的下钻应用的detail视图控制器。我有大约8个字段(如电话号码等)从。plist绘制。显然,这些占据了足够的空间,我可以用一点额外的空间。我猜它需要大约两个视图垂直的大小,但我不明白如何分配那种空间给一个UIScrollView。我读过的一些教程说,你甚至不需要在标题中定义它,我对此表示怀疑和不理解。此外,我不明白如何简单地让应用程序平滑地上下滚动。苹果的例子有固定的移动周期,在水平图片之间翻转。

我怀疑这有什么不同,但我有一张图片作为背景。我要在上面加载视图。

我的问题很宽泛,所以我不指望你花时间坐下来为我写出所有的代码(我也不会要求你这么做)。只是一个快速的大纲,有用的技巧将帮助我理解如何在我的项目上下文中加载这个视图。

多谢!

这相当简单。向视图添加一个UIScrollView。将字段等添加到滚动视图中。在viewDidLoad或类似的地方,你需要设置你的滚动视图的内容大小。这是一个可滚动的"虚拟"区域。这通常会大于滚动视图的框架。在您的例子中,您指出它应该大约是宽度的两倍。

例如,如果你有一个内部有视图的scrollview,并且你想通过滚动确保整个视图是可见的:

self.scrollView.contentSize = self.contentView.frame.size;
//setting scrollview zoom level
    self._scrollview.minimumZoomScale=0.5;
    self._scrollview.maximumZoomScale=6.0;
    self._scrollview.contentSize=CGSizeMake(320,500 );

你必须在。h类中输出滚动视图和@property @ synthesize这个滚动视图。然后你可以上下滚动,如果你只想要垂直滚动,那么你必须去界面构建器并取消勾选水平滚动

你可以为你的scrollview设置一些设置,以限制滚动水平或垂直。一些重要的是:

// pseudcode here, start typing "[scrollView " then press escape to get a intelli-sense of all // the things you can set for the scrollview.
// values could be YES/NO or TRUE/FALSE, I can't remember which one but 
// I think it's YES/NO. Once you start scrolling, the phone will determine
// which way you're scrolling then lock it to that direction
[scrollView setDirectionalLockEnabled:YES];
// when you slide the view, if enough of the next part of the view is visible, 
// the scrollview will snap or bounce the scrollview to fit this new "page".
// think of swiping feature to navigate the iPhone home screens 
// to show different "pages" of iphone apps
[scrollView setPagingEnabled:YES];
// as a safe guard, make sure the width of your scrollview fits snuggly with the content 
// it is trying to display. If the width is more than necessary to display your table of 
// data vertically, sometimes the scrollview will cause the 
// horizontal scrolling that you don't want to happen and you get bi-directional scrolling

只需在添加所有控件/按钮/文本字段等后设置UIScrollView的内容大小,例如您在UIScrollView中添加10个文本字段,那么UIScrollView的内容大小将为

lastTextField.frame.origin.y+lastTextField.frame.size.height+20; 20 is for margin.

就这样,如果你想知道更多关于你的应用的信息,请告诉我

最新更新