美文网首页
关于UICollectionViewFlowLayout布局的问

关于UICollectionViewFlowLayout布局的问

作者: HCL黄 | 来源:发表于2016-11-29 10:33 被阅读0次
  • 首先是我遇到的问题,如图:大概是说每个item的宽度必须小于UICollectionView的宽度减去部分insets左和右值,减去内容insets左和右值。


    QQ20161129-0@2x.png
  • 其次就是我的布局代码
  /** 懒加载 */
  - (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);
}
QQ20161129-1@2x.png
QQ20161129-2@2x.png
QQ20161129-3@2x.png
  • 效果图大概是这样的


    864483A3-7FB8-4EE6-9344-B72F42DBA43A.png
  • 一运行程序就出现了如上面所说的警告,程序员一看到警告就会忍不住想去处理它,然后我就开始填坑之路。。。
  • 最后解决是在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);
}
QQ20161129-0@2x.png

相关文章

网友评论

      本文标题:关于UICollectionViewFlowLayout布局的问

      本文链接:https://www.haomeiwen.com/subject/zkeapttx.html