问题描述
我有一个显示照片的 UICollectionView
.我已经使用 UICollectionViewFlowLayout
创建了集合视图.它工作得很好,但我想在边距上有间距.是否可以使用 UICollectionViewFlowLayout
或我必须实现自己的 UICollectionViewLayout
来做到这一点?
I have a UICollectionView
which shows photos. I have created the collectionview using UICollectionViewFlowLayout
. It works good but I would like to have spacing on margins. Is it possible to do that using UICollectionViewFlowLayout
or must I implement my own UICollectionViewLayout
?
推荐答案
您可以为您的 UICollectionView
使用 collectionView:layout:insetForSectionAtIndex:
方法或设置 附加到
属性:UICollectionView
的 UICollectionViewFlowLayout
对象的 >sectionInset
You can use the collectionView:layout:insetForSectionAtIndex:
method for your UICollectionView
or set the sectionInset
property of the UICollectionViewFlowLayout
object attached to your UICollectionView
:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(top, left, bottom, right);
}
或
UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[aFlowLayout setSectionInset:UIEdgeInsetsMake(top, left, bottom, right)];
为 Swift 5
更新func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 25, left: 15, bottom: 0, right: 5)
}
这篇关于UICollectionView 间距边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!