简体   繁体   English

调整 UICollectionView 中特定部分的插图

[英]Adjust insets of a specific section in UICollectionView

  1. insetForSectionAtIndex (on DelegateFlowLayout) enables one to set insets for all cells within a section insetForSectionAtIndex(在 DelegateFlowLayout 上)可以为一个部分中的所有单元格设置插图

  2. sectionInset (on FlowLayout) enables one to set insets that applies to all sections. sectionInset(在 FlowLayout 上)可以设置适用于所有部分的插图。

However, I am looking for a way of applying insets to only one specific section - is this possible?但是,我正在寻找一种仅将插图应用于一个特定部分的方法 - 这可能吗?

You must have to implement UICollectionViewDelegateFlowLayout to your class with this method:您必须使用此方法为您的类实现UICollectionViewDelegateFlowLayout

For Swift 4对于 Swift 4

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

    var edgeInsets = UIEdgeInsets()
    if section == THE_SECTION_YOU_WANT {
        // edgeInsets configuration stuff
    }

    return edgeInsets;
}

For Swift 3对于 Swift 3

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {

    var edgeInsets = UIEdgeInsets()
    if section == THE_SECTION_YOU_WANT {
        // edgeInsets configuration stuff
    }

    return edgeInsets;

}

for swift 4 is named: swift 4 被命名为:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
...

You'd do something like this from the UICollectionViewDelegateFlowLayout protocol:你可以从UICollectionViewDelegateFlowLayout协议做这样的事情:

func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        insetForSectionAt section: Int) -> UIEdgeInsets {

        switch section {
        case 0:
            return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        case 1:
            return UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
        }
    }

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

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