Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIRefreshControl not refreshing when triggered programmatically

I am trying to show a refresh control right when the view loads to show that I am getting getting data from Parse. The refresh control works as it should when the app is running, but I cannot get it to fire programmatically from anywhere in my app. This is the code that does not appear to run:

 override func viewDidAppear(animated: Bool) {
    self.refresher.beginRefreshing()
 }

Not only does this not run, but having the code in the app changes the attributes of the refresh control. When I have this code in the app, and show the refresher from user interaction, the refresh control does not have its attributed title as it usually does nor does it run the code that it should.

like image 383
Christian Ayscue Avatar asked Dec 25 '22 22:12

Christian Ayscue


1 Answers

I needed to add a delay between setContentOffset and the self.refreshControl?.sendActions(for: .valueChanged) call. Without the delay, the refreshControl's attributedTitle would not render.

This works in iOS 10 (Swift 3):

self.tableView.setContentOffset(CGPoint(x:0, y:self.tableView.contentOffset.y - (self.refreshControl!.frame.size.height)), animated: true)

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: {
  self.refreshControl?.sendActions(for: .valueChanged)
})
like image 160
David Crow Avatar answered Jan 19 '23 00:01

David Crow