这是一个简单的流布局,根据给定的列数、项目高度、项目间距、头部高度和尾部高度,在集合视图中以均匀的方式排列项目。
1. Add the files 'MONUniformFlowLayout.h' and 'MONUniformFlowLayout.m' to your project.
2. Then import the header file to use it.
1. Add 'pod MONUniformFlowLayout' in your Podfile.
2. Then 'pod install' in the terminal.
MONUniformFlowLayout *layout = [MONUniformFlowLayout new];
layout.interItemSpacing = MONInterItemSpacingMake(10.0f, 10.0f);
layout.enableStickyHeader = YES;
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds
collectionViewLayout:layout];
属性 interItemSpacing
是所有部分的项之间水平和垂直空间,而属性 enableStickyHeader
为用户提供了是否为所有部分启用或禁用粘性头部视图的选项。
静态内联方法 MONInterItemSpacingMake(CGFloat x, CGFloat y)
创建一个结构,用于定义项之间的水平和垂直空间。
@required
// Gets the item height in a particular section
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(MONUniformFlowLayout *)layout
itemHeightInSection:(NSInteger)section;
@optional
// Gets the header height in a particular section
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(MONUniformFlowLayout *)layout
headerHeightInSection:(NSInteger)section;
// Gets the footer height in a particular section
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(MONUniformFlowLayout *)layout
footerHeightInSection:(NSInteger)section;
// Gets the spacing between sections
- (CGFloat)collectionView:(UICollectionView *)collectionView
sectionSpacingForlayout:(MONUniformFlowLayout *)layout;
// Gets the number of columns in a particular section
- (NSUInteger)collectionView:(UICollectionView *)collectionView
layout:(MONUniformFlowLayout *)layout
numberOfColumnsInSection:(NSInteger)section;)
在 UIViewController 中的使用
@interface ViewController : UIViewController <MONUniformFlowLayoutDelegate,
UICollectionViewDelegate, UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout>
...
@end