Add Pull To Refresh to your iOS app using Swift

Last updated on: June 15, 2022

Today, I’ll show you how to add Pull To Refresh into your UITableView/UICollectionView very easily.

Adding Pull To Refresh in TableView/CollectionView

We already have an UITableView/UICollectionView filled with data, and we want to add the Pull To Refresh feature.

The only thing we have to do is to create an UIRefreshControl and add it to our UITableView/UICollectionView and trigger a method, to refresh the data, every time we swipe down.

var refControl = UIRefreshControl() override func viewDidLoad() { super.viewDidLoad() // Setting up UIRefreshControl refControl.tintColor = UIColor.white refControl.addTarget(self, action: #selector(handleRefresh(refreshControl:)), for: UIControl.Event.valueChanged) tableView.addSubview(refControl) // ... } @objc func handleRefresh(refreshControl: UIRefreshControl) { DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(1)) { // We add a 1-second delay for the pull to refresh animation because the UI will glitch otherwise and won't look nice // Refresh the data here DispatchQueue.main.async { self.tableView.reloadData() refreshControl.endRefreshing() } } }
Code language: Swift (swift)
You can find the final project here

If you have any questionsplease feel free to leave a comment below

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Get once a week my latest tutorials right in your inbox

Check your inbox to confirm your email