如何解决在刷新 UIRefreshControl 时使用 sectionHeadersPinToVisibleBounds 会导致粘性标题的位置不正确
将 UICollectionView
与 UICollectionViewFlowLayout
设置为 sectionHeadersPinToVisibleBounds
的 true
一起使用时,如果将 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()
}
}
}
是否有解决此问题的方法?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。