-
首先是我遇到的问题,如图:大概是说每个item的宽度必须小于UICollectionView的宽度减去部分insets左和右值,减去内容insets左和右值。
- 其次就是我的布局代码
/** 懒加载 */
- (UICollectionViewFlowLayout *)layout
{
if (_layout == nil) {
_layout = [[UICollectionViewFlowLayout alloc] init];
_layout.minimumLineSpacing = kMargin;
_layout.minimumInteritemSpacing = kMargin;
}
return _layout;
}
- (UICollectionView *)collectionView
{
if (_collectionView == nil) {
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = RGBA(230, 230, 230, 1);
// 注册cellHomeNormalCell
[_collectionView registerNib:[UINib nibWithNibName:@"HomeNormalCell" bundle:nil] forCellWithReuseIdentifier:HomeCellNormalID];
// 注册满屏的
[_collectionView registerNib:[UINib nibWithNibName:@"HomeFullCell" bundle:nil] forCellWithReuseIdentifier:HomeCellFullID];
// 注册有组头的cell
[_collectionView registerNib:[UINib nibWithNibName:@"HomeSectionCell" bundle:nil] forCellWithReuseIdentifier:HomeCellSectionID];
// 注册headerView
[_collectionView registerNib:[UINib nibWithNibName:@"HomeReusableHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HomeHeaderViewID];
}
return _collectionView;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 不需要额外的滚到区域
self.automaticallyAdjustsScrollViewInsets = NO;
// 添加collectionView
[self.view addSubview:self.collectionView];
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
// layout的frame
self.layout.itemSize = CGSizeMake(normalW, normalH);
self.layout.sectionInset = UIEdgeInsetsMake(0, kMargin, 0, kMargin);
// collectionView的Frame
self.collectionView.frame = CGRectMake(0, 64, kScreenW, kScreenH - kTabBarH);
}
-
效果图大概是这样的
- 一运行程序就出现了如上面所说的警告,程序员一看到警告就会忍不住想去处理它,然后我就开始填坑之路。。。
- 最后解决是在
UICollectionViewDelegateFlowLayout
代理里面有个方法是设置流水布局的UIEdgeInset
的,因为我在viewDidLayoutSubviews
里设置了layout.sectionInset
的左右间距为10,这样就导致了我正常布局的Cell是有间距的,但是我另外两个布局:“满屏Cell”和“满屏有其他内容的Cell”也跟随着有间距了,所有就出现了一开始打印的那些警告,因此删除在viewDidLayoutSubviews
对流水布局的设置,然后如图设置
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
// layout的frame
//self.layout.itemSize = CGSizeMake(normalW, normalH);
//self.layout.sectionInset = UIEdgeInsetsMake(0, kMargin, 0, kMargin);
// collectionView的Frame
self.collectionView.frame = CGRectMake(0, 64, kScreenW, kScreenH - kTabBarH);
}