使用 CompositionalLayout 时如何将 UICollectionView 部分固定到导航栏最好通过代码,但其他选项也不错

问题描述

我有一个函数来创建一个部分(我想锁定)。我知道如何固定部分标题(通过 pinToVisibleBounds) 我需要在滚动时固定部分(它应该粘在导航栏上)。 创建部分的代码

 private func createEmojiSection() -> NSCollectionLayoutSection {
        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),heightDimension: .fractionalHeight(1))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)
        item.contentInsets = NSDirectionalEdgeInsets(top: 0,leading: 8,bottom: 0,trailing: 8)
        let groupSize = NSCollectionLayoutSize(widthDimension: .estimated(120),heightDimension: .estimated(50))
        let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize,subitems: [item])
        let section = NSCollectionLayoutSection(group: group)
        section.orthogonalScrollingBehavior = .continuous
        section.contentInsets = NSDirectionalEdgeInsets(top: 10,leading: 12,bottom: 15,trailing: 12)
        let header = createHeader()
        section.boundarySupplementaryItems = [header]
        return section
    }

创建布局:

 private func createLayout() -> UICollectionViewLayout {
        let layout = UICollectionViewCompositionalLayout {[weak self](sectionIndex: Int,layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
            
            guard let section = self?.sections[sectionIndex] else { return nil }
            
            switch section.type {
            case "Numbers":
                return self?.createNumberSection()
            case "Emoji":
                return self?.createEmojiSection()
            case "Names":
                return self?.createStringSection()
            default:
                print("There is no such item")
                return nil
            }
        }
        layout.register(NameSectiondecorationView.self,fordecorationViewOfKind: ViewController.sectionBackgrounddecorationElementKind)
        return layout
    }

如果您需要在代码添加任何其他内容,请写在注释中。

我对所有单元格使用了 xib 并使用代码配置了集合。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)