美文网首页
自定义CollectionView中元素的大小和间距

自定义CollectionView中元素的大小和间距

作者: bu再等 | 来源:发表于2016-03-29 09:32 被阅读1074次

本人ios初学者,为自己学习方便,复制各位大神的学习性文章放在自己简书里,仅作为自己学习方便使用,如果作者疑此行为侵权,请随时联系本人删除,如有共同学习者复制此文章,请注明原出处

原文地址:http://www.cnblogs.com/davidgu/p/4975042.html

 让每个元素大小变为104 x 104Step 

1:在你的视图控制器头文件中实现遵守协议

@interface CollectionViewController ()<UICollectionViewDelegateFlowLayout>

@end

Step 2:

设置每个单元格的大小

eg:

复制代码

- (CGSize) collectionView:(UICollectionView *)collectionView

layout:(UICollectionViewLayout *)collectionViewLayout

sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

return CGSizeMake(104.0f, 104.0f);

}

复制代码

Step 3:

设置单元格间的横向间距

eg:

- (CGFloat) collectionView:(UICollectionView *)collectionView

layout:(UICollectionViewLayout *)collectionViewLayout

minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

{

return 2.0f;

}

Step 4:

设置纵向的行间距

eg:

- (CGFloat) collectionView:(UICollectionView *)collectionView

layout:(UICollectionViewLayout *)collectionViewLayout

minimumLineSpacingForSectionAtIndex:(NSInteger)section

{

return 2.0f;

}

step 5:

通过调整inset使单元格顶部和底部都有间距(inset次序: 上,左,下,右边)

eg:

- (UIEdgeInsets) collectionView:(UICollectionView *)collectionView

layout:(UICollectionViewLayout *)collectionViewLayout

insetForSectionAtIndex:(NSInteger)section

{

return UIEdgeInsetsMake(2.0f, 0.0f, 2.0f, 0.0f);

}

相关文章

网友评论

      本文标题:自定义CollectionView中元素的大小和间距

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