1.2 Flow Layout
1.2.1 核心概念
UICollectionViewFlowLayout是一个具体的layout对象,用来把item布局在网格中,并且可选页眉和页脚。在collection view中的items,可以从一行或者一列flow至下一行或者下一列(行或者列取决于滚动的方向)。每行都会根据情况,包含尽可能多的Cells。Cells可以是相同的尺寸,也可以是不同的尺寸。 下面是Flow Layout的一些特性 n 面向线性布局 n 可配置为网格 n 一组lines n 具有页眉和页脚 1.2.2 自定义 Flow Layout Flow Layout可以定制的主要功能如下 n Item size n Line spacing n Inter cell spacing n Scrolling direction n Header and footer size n Section Inset 下面我们来详细介绍这些可定制的功能。 这里需要对通过全局和delegate定制进行说明一下: 几乎所有的定制属性都是在UICollectionViewFlowLayout中,delegate实际上是collection view的delegate。UICollectionViewDelegateFlowLayout只是对UICollectionViewDelegate进行了一些扩展。 Item size(每个item的大小) 1、 可以进行全局配置,如下代码 @property(CGSize)itemSize layout.itemSize= CGSizeMake(30,20); 2、 也可以通过delegate对每一个item进行配置,如下代码 collectionView:layout:sizeForItemAt IndexPath: { return...; } 效果如下图所示 Line spacing(每行的间距) 1、 可以进行全局配置,如下属性 @property(CGFloat) minimumLineSpacing 2、 也可以通过delegate对每一个section进行配置,如下代码 ollectionView:layout:minimumLineSpacingForSectionAtIndex: 请按顺序看下面的三个图 Inter cell spacing(每行内部cell item的间距) 1、 可以进行全局配置,如下属性 @property(CGFloat) minimumInteritemSpacing 2、 也可以通过delegate对每一个section进行配置,如下代码 collectionView:layout:minimumInteritemSpacingForSectionAtIndex: 看看下面的两个图,蓝色是实际的item间距,绿色的是最小item间距。 Scrolling direction(滚动方向) 设置scrollDirection属性即可。两个值如下 UICollectionViewScrollDirectionVertical UICollectionViewScrollDirectionHorizontal 主要作用: n 定义了Flow Layout的基本行为 n 控制页眉页脚的维度 UICollectionViewScrollDirectionVertical效果如下图所示 UICollectionViewScrollDirectionHorizontal效果如下图所示 Header and footer size(页眉和页脚大小) 下面是页眉和页脚的一些解释。 n 也就是supplementary views n 通过数据源的方法来提供内容,如下 -collectionView:viewForSupplementaryElementOfKind:atIndexPath: n 两种常量(类型) UICollectionElementKindSectionHeader UICollectionElementKindSectionFooter n 同样需要注册一个类并从队列中取出view - registerClass:forSupplementaryViewOfKind:withReuseIdentifier: -registerNib:forSupplementaryViewOfKind:withReuseIdentifier: -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: 页眉和页脚的size配置方式: 1)可以全局配置,如下属性 @property(CGSize) headerReferenceSize @property(CGSize) footerReferenceSize 2)也可以通过delegate对每一个section进行配置,如下代码 collectionView:layout:referenceSizeForHeaderInSection: collectionView:layout:referenceSizeForFooterInSection: 页眉页脚的属性如下图 当垂直的时候,需要设置Height,如下图 当水平的时候,需要设置Width,如下图 Section Inset 我们先通过两个图来看看Sections Insets是怎么回事 从上面的两个图中,我们大概知道了,Section Inset就是某个section中cell的边界范围。 SectionInset的配置方式同样有两种 1、 通过全局配置,如下属性 @propertyUIEdgeInsets sectionInset; 2、 也通过delegate对每一个section进行配置,如下函数 - (UIEdgeInsets)collectionView:layout:insetForSectionAtIndex: |