如何以编程方式将UIPageController和UICollectionViewController添加到UIViewController

问题描述 投票:-1回答:1

我已经以编程方式制作了一个UIViewController,并且希望在底部具有4个单元格的集合视图,但是我希望能够在集合视图的不同页面上滑动以查看不同的单元格。我不确定从何处开始在集合视图上启用分页,以及如何在设置单元格或创建页面控制器并将集合视图添加到页面上的工作?我已经在网上看到了几种方法,但这些方法确实无法满足我的需求。

我想要这样的东西:

让我知道是否能为您提供更多信息。我只是创建了一个基本的页面控制器,但不确定如何实现所需的内容。

我的页面控制器代码:

let friendsPageController: UIPageControl = {
    let control = UIPageControl()
    control.numberOfPages = 4
    control.currentPage = 0
    control.tintColor = UIColor.blue
    control.pageIndicatorTintColor = UIColor.lightGray
    control.currentPageIndicatorTintColor = UIColor.white
    control.backgroundColor = UIColor.blue
    return control
}()
swift uiview uicollectionview uipageviewcontroller
1个回答
0
投票

您需要的只是一个UICollectionViewController或我假设您拥有的,符合collectionView协议的UIViewController。带有一个UICollectionView的UIViewController是一个很好的起点。但是,您插入的单元格需要不同。首先,ViewController中的collectionView必须具有collectionView?.isPagingEnabled = true及其布局scrollDirection设置为。horizo​​ntal。完成后,您需要创建单元格。

您实现的单元格应该是UICollectionViewControllerUICollectionViewDelegateFlowLayout的子类。尽管我认为您无论如何都不会在此视图中滚动,但该collectionView的布局需要将scrollDirection设置为。vertical。此collectionView中的单元格数将为4。然后,您要dequeue您要拥有4个单元格的单元格(头为蓝色,白色为白色)。

UICollectionViewController然后需要出列作为第一个collectionView的单元格。基本上:

您的主ViewController具有水平滚动的collectionView。此collectionView具有UICollectionViewController作为单元格并垂直滚动。

听上去很复杂,但是我已经做到了,并且运行顺利。如果您有任何问题,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.