UIScrollView中实现不可滚动的UICollection

UIScrollView中实现不可滚动的UICollection

本文介绍了如何在UIScrollView中实现不可滚动的UICollectionView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我想要的屏幕布局基本上是用户配置文件(iOS 7 + Xcode 5)。我使用UIScrollView作为顶级视图。 原因是我希望其所有子视图都滚动(用户信息视图 - 带有个人资料图片的视图和您在屏幕上看到的一些按钮,以及照片集合视图 - 带有滚动时的黑色背景。

In shorts, my desired screen layout is basically a user profile (iOS 7 + Xcode 5). I used UIScrollView as the top level view. The reason is that I want all its subviews to scroll (user info view - the view with a profile image and some buttons you see on the screen, and the photos collection view - the one with black background) when it is scrolled.

黑色背景区域将显示用户照片。我想知道我是否可以在这里使用UICollectionView,或者有更好的方法来实现它。在这种情况下,UICollectionView不能自己滚动,它只显示所有单元格,而滚动工作由最外层的UIScrollView处理。

The region with black background will show user photos. I'm wondering if I could use a UICollectionView here, or there's a better way to implement it. The UICollectionView in this case shouldn't be able to scroll itself, it just shows all cells, while the scrolling work is handled by the outermost UIScrollView.

我阅读了以下帖子:

有人说在UIScrollView中实现UICollectionView是不可能的(或者至少是奇怪的),因为UIScrollView是UICollectionView的超类这会导致意外行为。有人说应该以另一种方式实施(但我没有看到明确的建议)。

Some said it's not possible (or at least, weird) implemeting UICollectionView inside UIScrollView because UIScrollView is UICollectionView's superclass which leads to unexpected behaviour. Some said it should be implemented in another way (but I didn't see a clear suggestion).

推荐答案

是的,你可以在 UIScrollView 中放置一个 UICollectionView 。自iOS 3.0起,和 UICollectionView UIScrollView 的子类。例如,查看iOS设备上的App Store应用程序。屏幕垂直滚动 - 它是 UIScrollView UITableView (它本身是的UIScrollView )。每行图标都水平滚动 - 每行都是 UICollectionView

Yes, you can put a UICollectionView inside a UIScrollView. iOS has fully supported nested scroll views since iOS 3.0, and UICollectionView is a subclass of UIScrollView. For example, check out the App Store app on your iOS device. The screen scrolls vertically - it's either a UIScrollView or a UITableView (which is itself a subclass of UIScrollView). And each row of icons scrolls horizontally - each row is a UICollectionView.

然而,目前还不清楚为什么需要在滚动视图中放置一个集合视图。听起来你只想让照片视图滚动,所以只需将照片视图设为集合视图即可。为什么需要将集合视图放在滚动视图中?

However, it's not clear why you need to put a collection view inside a scroll view. It sounds like you only want the photos view to scroll, so just make the photos view be a collection view. Why do you need to put the collection view inside a scroll view?

只需使用集合视图。将第0部分的标题设置为配置文件信息视图。您不需要滚动视图。

Just use a collection view. Set the header of section 0 to the profile info view. You don't need a scroll view.

如果您将所有照片放在一个部分中,则可以在故事板中设置没有代码的标题。如果你使用多个部分,你需要在你的委托中实现 collectionView:layout:referenceSizeForHeaderInSection: collectionView:viewForSupplementaryElementOfKind:atIndexPath:在您的数据源中。

If you put all the photos in one section, you can set up the header in your storyboard with no code. If you use multiple sections, you'll need to implement collectionView:layout:referenceSizeForHeaderInSection: in your delegate and collectionView:viewForSupplementaryElementOfKind:atIndexPath: in your data source.

这篇关于如何在UIScrollView中实现不可滚动的UICollectionView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:03