Search code examples
swiftuitableviewuiscrollviewuirefreshcontrol

Refresh control not sliding screen back up after refreshing in Swift


I have added a refresh control to my table view controller and when I slide down the refreshing animation occurs and my refresh code runs. However after it finishes refreshing (Spinning animation gone) the table that i swiped down does not scroll back up and essentially there is empty white space.

This is the code I used to implement my refresh control.

    refreshControl = UIRefreshControl()
    refreshControl.backgroundColor = UIColor.white

    refreshControl.addTarget(self, action: #selector(HomeViewController.refresh), for: UIControlEvents.valueChanged)
    if #available(iOS 10.0, *) {
      tableView.refreshControl = refreshControl
    } else {
      tableView.backgroundView = refreshControl
    }

And this is my refresh function

func refresh() {
    Feed.query(params) { feeds, error in
      self.refreshControl.endRefreshing()
      if error == nil {
        self.oldIndex = nil
        self.feeds = feeds
        self.scrollViewDidScroll(self.tableView)
        self.tableView.reloadData()
        self.edgesForExtendedLayout = []
        self.checkIfEmpty()
      } else {
        Alert(view: self).show(error!)
      }
    }
  }

After I get data from my call then I endRefreshing. Thanks! I am currently testing on iOS 10.2 simulator.


Solution

  • Try enclosing your endRefreshing code in main thread to see if it works:

    DispatchQueue.main.async { self.refreshControl.endRefreshing() }