首页 > 解决方案 > iOS UICollectionView 推送刷新时的意外行为

问题描述

我正在尝试在我的 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

在此处输入图像描述

标签: iosswiftuicollectionviewuikit

解决方案


无需设置框架UIRefreshControl

我通常使用以下步骤来实现它:

在类属性块中:

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
}

希望能帮助到你。

PS 有时会出现刷新控制闪烁错误(与您的有些相似)。可以通过将:添加extendedLayoutIncludesOpaqueBars = true到您的viewDidLoad()方法来解决。


推荐阅读