我尝试在我的collectionView上实现推送刷新,代码就是它。
private lazy var refreshControl : UIRefreshControl = {
let frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let control = UIRefreshControl(frame: frame)
control.addTarget(self, action: #selector(getAllJobs), for: .valueChanged)
return control
}()
collectionView.refreshControl = refreshControl
最佳答案
UIRefreshControl
无需设置frame
我通常使用以下步骤来实现它:
在类属性 block 中:
let refreshControl: UIRefreshControl = UIRefreshControl()
在viewDidLoad
中:
collectionView.alwaysBounceVertical = true
collectionView.refreshControl = refreshControl
refreshControl.addTarget(self, action: #selector(loadData), for: .valueChanged)
类主体中的某处:
// Refresh handler
@objc func loadData() {
// Your refresh-code here
}
希望有帮助。
附注有时会出现刷新控制闪烁错误(某种程度上与您的类似)。可以通过在 viewDidLoad()
方法中添加 extendedLayoutInincludesOpaqueBars = true
来解决。
关于iOS UICollectionView 推送刷新时出现意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53084506/