UICollectionView Set number of columns

后端 未结 17 1161
执笔经年
执笔经年 2020-11-28 17:28

I just started learning about UICollectionViews. I\'m wondering if anyone knows how to specify the number of columns in a collectionview. The default is set to 3 (iPhone/por

17条回答
  •  迷失自我
    2020-11-28 18:26

    the perfect solution is to Using UICollectionViewDelegateFlowLayout but you can easily so calculate the cell width and divided on the wanted number of columns you want

    the tricky is to make the width with no fraction

    (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    
    {
     CGFloat screenWidth = self.view.frame.size.width;
       CGFloat marginWidth = (screenWidth - collectionView.frame.size.width);
    
    
       CGFloat cellWith = (collectionView.frame.size.width - marginWidth )/3;
       cellWith= floorf(cellWith);
    
    
    
    
      CGSize retval = CGSizeMake(cellWith,cellWith);
    
    
      return retval;}
    

提交回复
热议问题