如何自定义CollectionView中每个元素的大小和间距
3个回答
展开全部
Step 1:
在你的视图控制器头文件中实现UICollectionViewFlowLayout协议
eg:
@interface XXViewController : UICollectionViewController<UICollectionViewFlowLayout>
...
@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);
}
在你的视图控制器头文件中实现UICollectionViewFlowLayout协议
eg:
@interface XXViewController : UICollectionViewController<UICollectionViewFlowLayout>
...
@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);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Step 1:
在你的视图控制器头文件中实现UICollectionViewFlowLayout协议
eg:
@interface XXViewController : UICollectionViewController<UICollectionViewFlowLayout>
...
@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);
}
在你的视图控制器头文件中实现UICollectionViewFlowLayout协议
eg:
@interface XXViewController : UICollectionViewController<UICollectionViewFlowLayout>
...
@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);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询