在刷新 UIRefreshControl 时使用 sectionHeadersPinToVisibleBounds 会导致粘性标题的位置不正确

问题描述

UICollectionViewUICollectionViewFlowLayout 设置为 sectionHeadersPinToVisibleBoundstrue 一起使用时,如果将 UIRefreshControl 添加到集合中,粘性标题的位置将不正确景色令人耳目一新。

示例视图控制器:

import UIKit

public final class StickyHeaderTestViewController: UICollectionViewController {
    init() {
        let layout = UICollectionViewFlowLayout()
        layout.sectionHeadersPinToVisibleBounds = true

        super.init(collectionViewLayout: layout)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override public func viewDidLoad() {
        super.viewDidLoad()

        collectionView.backgroundColor = .white
        collectionView.register(UICollectionViewCell.self,forCellWithReuseIdentifier: "UICollectionViewCell")
        collectionView.register(UICollectionReusableView.self,forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,withReuseIdentifier: "UICollectionReusableView")

        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self,action: #selector(refreshControlValueChanged(_:)),for: .valueChanged)
        collectionView.refreshControl = refreshControl
    }

    override public func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: view.bounds.width,height: 40)
        layout.headerReferenceSize = CGSize(width: view.bounds.width,height: 40)
    }

    override public func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UICollectionViewCell",for: indexPath)
        switch indexPath.section {
        case 0:
            cell.backgroundColor = .blue
        case 1:
            cell.backgroundColor = .yellow
        case 2:
            cell.backgroundColor = .blue
        default:
            break
        }
        return cell
    }

    override public func collectionView(_ collectionView: UICollectionView,viewForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableView {
        let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind,withReuseIdentifier: "UICollectionReusableView",for: indexPath)
        switch indexPath.section {
        case 0:
            view.backgroundColor = .brown
        case 1:
            view.backgroundColor = .green
        case 2:
            view.backgroundColor = .brown
        default:
            break
        }
        return view
    }

    override public func numberOfSections(in collectionView: UICollectionView) -> Int {
        3
    }

    override public func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        10
    }

    @objc private func refreshControlValueChanged(_ sender: UIRefreshControl) {
        dispatchQueue.main.asyncAfter(deadline: .Now() + 10) {
            sender.endRefreshing()
        }
    }
}

是否有解决此问题的方法

Offset sticky header

解决方法

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

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

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