iOS Modern Collection View
TL;DR
- 使用的技术: Compositional layout + Diffable data source。iOS 14+。
- 创建 layout 以描述布局;
- 创建 dataSource 以提供数据和 view:
- 使用 CellRegistration 和 dequeueConfiguredReusableCell 来获取 cell;
- 使用 SupplementaryRegistration 和 dequeueConfiguredReusableSupplementary 来获取 header、footer 等 supplementary views。
Compositional Layout

如上图所示,一个 compositional layout 由若干个 section 组成,每个 section 又由若干个 group 组成,每个 group 又包含若干个 item,因此,创建一个 layout 的过程即先创建最小展示单元 item 开始,再创建 group,创建 section,创建 layout:
func createBasicListLayout() -> UICollectionViewLayout {
let itemSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0)
)
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(44)
)
let group = NSCollectionLayoutGroup.horizontal(
layoutSize: groupSize,
subitems: [item]
)
let section = NSCollectionLayoutSection(group: group)
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
SectionProvider
如果需要多个布局不同的 sections,可以用下面的构造函数来创建 layout:
// UICollectionViewCompositionalLayout
init(
sectionProvider: @escaping UICollectionViewCompositionalLayoutSectionProvider,
configuration: UICollectionViewCompositionalLayoutConfiguration
)
其中闭包 sectionProvider 用于创建并返回 layout 的各个 secitons,声明如下:
typealias UICollectionViewCompositionalLayoutSectionProvider = (Int, NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection?
// arguments meaning: { sectionIndex, layoutEnvironment in ... }
该闭包也会在一些系统事件触发时被调用,如横竖屏切换、系统字体变更、iPad multitasking 引发的 size class 变更等,可以访问 layoutEnvironment 来获取相关信息,以适配这些场景。
LayoutConfiguration
UICollectionViewCompositionalLayoutConfiguration
scrollDirection: UICollectionView.ScrollDirection- 滚动方向:
.vertical|.horizontal
- 滚动方向:
interSectionSpacing: CGFloat- Sections 之间的间距
contentInsetsReference: UIContentInsetsReference- 定义 content inset 时的参考边界
.automatic|.none|.safeArea|.layoutMargins|.readableContent
- 红色为 safeArea,绿色为 layoutMargins,蓝色为 readableContent
boundarySupplementaryItems: [NSCollectionLayoutBoundarySupplementaryItem]- 在整个 layout 边界处的一些附加 items,如全局 header、全局 footer、badge 等
LayoutEnvironment
包含 layout container 和 environment traits 的信息
container: NSCollectionLayoutContainercontentSize: CGSizeeffectiveContentSize: CGSize:content insets 生效之后的 sizecontentInsets: NSDirectionalEdgeInsetseffectiveContentInsets: NSDirectionalEdgeInsets
traitCollection: UITraitCollection
List layout
Compositional layout 专门提供了一个接口来创建 list layout:
static func list(using: UICollectionLayoutListConfiguration) -> UICollectionViewCompositionalLayout
UICollectionLayoutListConfiguration 用于配置 list layout 的一些属性,构造函数中需要指定一个 appearance,如 .plain,.sidebar,.grouped 等。参见 Apple 文档 来查看可配置的属性,下面列出几种。
- headerMode
.none:不展示 header;.supplementary:使用 supplementary views 来展示 headers;.firstItemInSection:使用 section 中的第一个 item 来作为 header。
Cell view 和 supplementary view 的获取
创建 dataSource 的时候,需要传入一个 cellProvider 来提供 cell view:
dataSource = UICollectionViewDiffableDataSource<Int, UUID>(collectionView: collectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, itemIdentifier: UUID) -> UICollectionViewCell? in
// Configure and return cell.
}
之前我们需要调用 register 方法来注册所需要的 cell 类,然后在 cellProvider 中调用 dequeueReusableCell 方法来获取复用的 cell。iOS 14 提供了一组新的方法,通过 CellRegistration 来配置 cell,然后使用 dequeueConfiguredReusableCell 方法来获取复用的 cell,在调用该方法的时候会自动将 cell 注册到 collectionView 上。如:
let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Int> { cell, indexPath, item in
var contentConfiguration = cell.defaultContentConfiguration()
contentConfiguration.text = "\(item)"
contentConfiguration.textProperties.color = .lightGray
cell.contentConfiguration = contentConfiguration
}
dataSource = UICollectionViewDiffableDataSource<Section, Int>(collectionView: collectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, itemIdentifier: Int) -> UICollectionViewCell? in
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration,
for: indexPath,
item: itemIdentifier)
}
注意不要在 cellProvider 里面创建 cell registration,会造成 cell 无法复用,且在 iOS 15+ 上直接报异常
对 cell 的配置通过 UIListContentConfiguration 来完成,调用 cell 的 defaultContentConfiguration 来获取一个包含一些预设属性的 configuration,根据传入的数据对其进行配置,然后再赋值回 cell 的 contentConfiguration。设置 contentConfiguration 会替换掉 cell 现有的 contentView。Cell configuration 具体可参见 Modern cell configuration (WWDC20 10027)。
Header、footer 等 supplementary views 的配置和 cell 类似,设置 dataSource 的 supplementaryViewProvider 属性来提供用于创建 view 的闭包,在闭包里面调用 dequeueConfiguredReusableSupplementary 来获取一个复用的 view,该方法接受一个闭包参数 SupplementaryRegistration 来配置 view。
References
- https://www.vbutko.com/articles/how-to-create-uicollectionview-list-with-compositional-layout/
- https://jeandavidpeirolo.medium.com/how-uilayoutguide-can-help-you-manage-ui-complexity-8e911c9bcdd7
iOS Modern Collection View的更多相关文章
- Collection View Programming Guide for iOS---(一)----About iOS Collection Views
Next About iOS Collection Views 关于iOS Collection Views A collection view is a way to present an orde ...
- IOS UIView 03- 自定义 Collection View 布局
注:本人是翻译过来,并且加上本人的一点见解. 前言 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一颗新星.它和 UITableView 共享一套 API ...
- iOS系列译文:自定义Collection View布局
原文出处: Ole Begemann 译文出处: 黄爱武(@answer-huang).欢迎加入技术翻译小组. UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一 ...
- Collection View Programming Guide for iOS---(七)---Custom Layouts: A Worked Example
Custom Layouts: A Worked Example Creating a custom collection view layout is simple with straightfor ...
- Collection View Programming Guide for iOS---(五)---Incorporating Gesture Support
Incorporating Gesture Support 结合手势支持 You can add greater interactivity to your collection views th ...
- Collection View Programming Guide for iOS---(三)---Designing Your Data Source and Delegate
Designing Your Data Source and Delegate 设计你的数据源和委托 Every collection view must have a data source o ...
- Collection View 自定义布局(custom flow layout)
Collection view自定义布局 一般我们自定义布局都会新建一个类,继承自UICollectionViewFlowLayout,然后重写几个方法: prepareLayout():当准备开始布 ...
- iOS—使用picker View
iOS—使用picker View 一.实现效果 说明:点击随机按钮,能够自动选取,下方数据自动刷新. 二.实现思路 1.picker view的有默认高度为162,不可修改. 2.显示数据,需要设置 ...
- 自定义 Collection View 布局
自定义 Collection View 布局 answer-huang 29 Mar 2014 分享文章 UICollectionView 在 iOS6 中第一次被引入,也是 UIKit 视图类中的一 ...
- Collection View Programming Guide for iOS---(六)---Creating Custom Layouts
Creating Custom Layouts 创建自定义布局 Before you start building custom layouts, consider whether doing so ...
随机推荐
- mysql5.7 大量sleep连接解决方法
show processlist 查看发现有大量sleep进程 查看当前数据库设置的最大连接数 show variables like 'max_connections'; 如果是生产环境需要紧急处理 ...
- 西门子SIMATIC LPMLV30 库的模式和状态管理器
从基于S7-1200 / S7-1500的OMAC PackML V3.0获取到的文章内容,用于记录查看 基本信息 根据PackML_V3.0,该库包含了用于机械模式和状态管理器的功能模块. • 机械 ...
- 【framework】Task简介
1 前言 Task 即任务,一个 Task 对应一个 Activity,其父容器为 TaskStack,子容器为 AppWindowToken. 关于其父类及祖父类的介绍,见 → Window ...
- Context与Reducer
Context与Reducer Context是React提供的一种跨组件的通信方案,useContext与useReducer是在React 16.8之后提供的Hooks API,我们可以通过use ...
- Swoole从入门到入土(14)——HTTP服务器[协程风格]
在这之间,我们讨论了异步风格HTTP服务器,它继承自异步风格的TCP服务器,有着异步风格的所有优点,同时如果碰到了大量的IO等待也会存在阻塞的缺点. 而协程风格的HTTP则很好处理了IO等待的问题.这 ...
- 细说 QUEST CENTRAL FOR DB2 八宗罪
作为一个从事oracle plsql开发2年的程序员,如今跳槽从事DB2数据仓库项目. 以PL/SQL Developer为参考,以下简称PLSQL,细说QUEST CENTRAL FOR DB2 5 ...
- Vmware中Linux通过NAT设置静态IP实现上网
1.设置虚拟机上网方式为NAT 2.修改centos网络配置文件,我的是centos7.4,主要网关不能和主机设置的一致 [root@dylan-centos ~]# vi /etc/sysconfi ...
- Hexo - 搭建个人博客的bug集合
按照很多视频教程进行操作,发现到hexo d这一步后,无法部署到github远端. 目前的解决方法: npm un hexo-deployer-git npm i hexojs/hexo-deploy ...
- less变量书写及样式混入
定义变量 定义混入样式 变量及混入样式使用 样式文件中 ~@代表src
- MySQL单表能存储多少条数据?
MySQL是中小型网站普遍使用的数据库之一,然而,很多人并不清楚MySQL到底能支持多大的数据量,甚至对它产生误解.MySQL单表的上限,主要与操作系统支持的最大文件大小有关.事实上MySQL能承受的 ...