小编典典

UICollectionView 间距边距

all

我有一个UICollectionView显示照片的。我已经使用UICollectionViewFlowLayout.
它工作得很好,但我想在边距上有间距。是否可以使用UICollectionViewFlowLayout或必须自己实现UICollectionViewLayout


阅读 94

收藏
2022-07-14

共1个答案

小编典典

您可以使用您的collectionView:layout:insetForSectionAtIndex:方法UICollectionView或设置附加到您sectionInset的对象的属性:UICollectionViewFlowLayout``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)
    }
2022-07-14