How do I add a UIRefreshControl to a UITableView programmatically?

273 Views Asked by At

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?

Edit: I have narrowed it down to .prefersLargeTitles = true Removing it fixes the problem, but I want to keep large titles

2

There are 2 best solutions below

0
JAINESH DOSHI On

You have to add refreshControl as below

tableView.addSubview(refreshControl)
0
Jayachandra A On

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()
}