Home > Blockchain >  How do I add a UIRefreshControl to a UITableView programmatically?
How do I add a UIRefreshControl to a UITableView programmatically?

Time:06-15

I am trying to add a UIRefreshControl to a UITableView, which is in a UINavigationController. I have tried setting tableView.refreshControl = refreshControl, but it doesn't work properly. How can I properly add a UIRefreshControl without a storyboard?

CodePudding user response:

You have to add refreshControl as below

tableView.addSubview(refreshControl)

CodePudding user response:

Here the simple code to add UIRefreshControl to a UITableView

var refreshControl: UIRefreshControl!
refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(refreshData), for: .valueChanged)
tableView.addSubview(refreshControl)

@objc func refreshData() {
 print(#function)
 // End the refreshControl after completing your task
 refreshControl.endRefreshing()
}
  • Related