Swift 4: Pull To Refresh / UIRefreshControl

TableViews can have a useful feature to refresh the data inside of them, simply pull to refresh. Apple didn’t invent this, that was Loren Brichter of Tweetie, which was sold to Twitter and Twitter was eventually granted a Patent for the feature. Today this feature is absolutely ambiguous and very easy to implement.

For example, if you add the following code to your viewDidLoad() Function

override func viewDidLoad() { 
    super.viewDidLoad()
    //more code
}

This will create a blue background and use white color for the spinner and text. When the pull to refresh gets executed it will execute the reloadData() function.

let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(reloadData), for: UIControl.Event.valueChanged)
refreshControl.backgroundColor = UIColor(red:0.004, green:0.5, blue: 1.0, alpha: 0.75 )
refreshControl.tintColor = UIColor.white
let attributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
refreshControl.attributedTitle = NSAttributedString(string: "Refreshing Planes Data", attributes: attributes)
tableView.refreshControl = refreshControl

 

Swift 4: Pull To Refresh / UIRefreshControl
Scroll to top