Search code examples
iosswiftuirefreshcontrol

UIRefreshControl does not work correctly


The refresh wheel stops at 95% full, and the refresh will only work if from the very top of the screen you reach the bottom.

lazy var refreshControl: UIRefreshControl = {
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action:
       #selector(VFeedVC.refresh(_:)),
                            for: .valueChanged)
    return refreshControl
}()

override func viewDidLoad() {
    self.tableView.addSubview(refreshControl)
}

func refresh(_ sender: Any) {
    Metric.sendEvent("RefreshFeed")
    self.refreshControl.beginRefreshing()
    self.getFeeds(self.user.id)
}

func getFeeds(_ userId: Int) {
    VNetwork.getFeeds(userId) { feeds in
        self.feeds = feeds
        self.tableView.isHidden = self.feeds.count == 0
        self.configureCells()
        self.refreshControl.endRefreshing()
        self.tableView.reloadData()
    }
}

enter image description here


Solution

  • This works for me on iOS 11:

    override func viewDidLoad() {
        self.tableView.refreshControl = refreshControl
        self.tableView.addSubview(refreshControl)
    }
    

    Also, you don't need to explicitly tell the refresh control to begin refreshing, so this is enough:

    func refresh(_ sender: Any) {
        Metric.sendEvent("RefreshFeed")
        self.getFeeds(self.user.id)
    }
    

    Also make sure that in the callback for VNetwork.getFeeds(userId) { feeds in you are on the main thread.