ios - collectionview global header 不会将单元格向下推而是保持在顶部

标签 ios swift uicollectionview uikit uicollectionviewcompositionallayout

我有一个 Collection View 标题(蓝色)和单元格(红色)。我希望能够以编程方式显示/隐藏标题,但是当我以编程方式显示标题时,它会出现在单元格的顶部(或使 ScrollView 下降一点)。我希望标题将整个 ScrollView 向下推,这样我就不必在单击“切换标题”后向上滚动。

enter image description here

我非常努力地为下面的问题重现最少的代码。请分享任何见解。

struct Section: Hashable {
    var items: [Int]
}

class ViewController: UIViewController {

    var collectionView: UICollectionView!
    var dataSource: UICollectionViewDiffableDataSource<Section, Int>?
    
    var showHeader = true
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView = UICollectionView(frame: view.frame, collectionViewLayout: createCompositionalLayout())
        collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        collectionView.backgroundColor = .systemBackground
        
        collectionView.register(CellView.self, forCellWithReuseIdentifier: "cellId")
        collectionView.register(HeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerId")
        
        view.addSubview(collectionView)

        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
        collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        
        createDataSource()
        addData()
    }

    func createCompositionalLayout() -> UICollectionViewLayout {
        // layout for cell
        let layout = UICollectionViewCompositionalLayout { sectionIndex, layoutEnvironment in
            let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))
            let layoutItem = NSCollectionLayoutItem(layoutSize: itemSize)
            layoutItem.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 0, bottom: 10, trailing: 0)
            let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(200))
            let layoutGroup = NSCollectionLayoutGroup.vertical(layoutSize: layoutGroupSize, subitems: [layoutItem])
            let layoutSection = NSCollectionLayoutSection(group: layoutGroup)
            return layoutSection
        }
        
        let config = UICollectionViewCompositionalLayoutConfiguration()
        
        if showHeader {
            let layoutSectionHeader = createGlobalHeader()
            config.boundarySupplementaryItems = [layoutSectionHeader]
        }
        
        layout.configuration = config
        return layout
    }
    
    func createGlobalHeader() -> NSCollectionLayoutBoundarySupplementaryItem {
        let layoutSectionHeaderSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50))
        
        let layoutSectionHeader = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: layoutSectionHeaderSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
        layoutSectionHeader.pinToVisibleBounds = true
        
        return layoutSectionHeader
    }
    
    func createDataSource() {
        dataSource = UICollectionViewDiffableDataSource<Section, Int>(collectionView: collectionView) { collectionView, indexPath, item in
            guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as? CellView else { fatalError("Unable to dequeue ") }
            cell.button.addTarget(self, action: #selector(self.onButtonClick), for: .touchUpInside)
            return cell
        }
        
        dataSource?.supplementaryViewProvider = { (collectionView, kind, indexPath) in
            guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerId", for: indexPath) as? HeaderView else { return nil }
            return header
        }
    }
    
    @objc func onButtonClick() {
        print("toggle")
        showHeader.toggle()
        collectionView.collectionViewLayout = createCompositionalLayout()
        collectionView.layoutIfNeeded()
    }
    
    func addData() {
        var snapshot = NSDiffableDataSourceSnapshot<Section, Int>()
        var sections: [Section] = []
        sections.append(Section(items: [0,1,2,3,4,5,6,7,8,9,10,11,12]))
        
        snapshot.appendSections(sections)
        
        for section in sections {
            snapshot.appendItems(section.items, toSection: section)
        }
        
        dataSource?.apply(snapshot)
    }

}

class CellView: UICollectionViewCell {
    let button = UIButton()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .red
        
        addSubview(button)
        button.setTitle("Toggle header", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.topAnchor.constraint(equalTo: topAnchor).isActive = true
        button.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class HeaderView: UICollectionReusableView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .blue
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

最佳答案

如果我答对了你的问题,你可以将 Collection View 滚动到顶部:

collectionView.setContentOffset(.zero, animated: false)

任何时候你想显示标题。 这是一种解决方法,但我认为它应该可以解决问题。

关于ios - collectionview global header 不会将单元格向下推而是保持在顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65422920/

相关文章:

ios - 惊人的音频引擎如何将过滤器应用于麦克风输入

iphone - NSMutableString appendFormat ... 添加空格?

ios - 独立填充 Collection View 的每个部分 Swift

ios - 以编程方式在 UITableViewCell 内的 UICollectionView 内自定义 UICollectionViewCell

ios - 仅当加载所有数据时才加载 CollectionView 单元格

ios - 如何在 NXOAuth2 中使用保存的 token ?

ios - 在不同的collectionView单元格内填充不同的tableView

ios - 如何更改 Xcode Playground 上的语言环境

swift - 通过拖动 View 边缘来变换 View (Swift)

iOS:当分段控件更改时隐藏或删除以编程方式添加的 subview