繁体   English   中英

Swift 4-集合视图将节插图应用于标题吗?

[英]Swift 4 - Collection View Apply Section insets to header?

我正在使用“收藏夹视图”,并且能够向“收藏夹视图”内部的单元格中添加“节”插图,但是似乎不适用于“收藏夹视图”的可重用视图,是否可以为可重复使用的视图设置节插图?

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {


        if(kind == UICollectionView.elementKindSectionFooter)
        {
            let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "ReviewsFooter", for: indexPath) as! ReviewFooterCell

            footerView.reviewFooterDelegate = self

            return footerView
        }
        else if(kind == UICollectionView.elementKindSectionHeader)
        {
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "ReviewHeader", for: indexPath) as! ReviewHeaderCell

            headerView.totalReviews.text = "Total Reviews: " + String(self.reviews.count)

            return headerView
        }

        fatalError()

    }

首先,让我们弄清楚什么是Section Insets

节插图是仅应用于节中项目的边距。 它们代表页眉视图和第一行项目之间以及末行和页脚视图之间的距离。 它们还指示单行项目两侧的间距。 它们不会影响页眉或页脚本身的大小。

您可以通过以下方法在collectionView添加section insets

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM