0% found this document useful (0 votes)
20 views3 pages

Using UIRefreshControl

The document discusses how to add a pull to refresh feature to scroll views like UIScrollView, UITableView, and UICollectionView using UIRefreshControl. It involves creating a UIRefreshControl instance variable, adding it to the scroll view in viewDidLoad, and implementing an onRefresh method that ends refreshing after a delay.

Uploaded by

Bảo Kabi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Using UIRefreshControl

The document discusses how to add a pull to refresh feature to scroll views like UIScrollView, UITableView, and UICollectionView using UIRefreshControl. It involves creating a UIRefreshControl instance variable, adding it to the scroll view in viewDidLoad, and implementing an onRefresh method that ends refreshing after a delay.

Uploaded by

Bảo Kabi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Prework (/prework)

Week 1
Overview (/content/week_1/overview)
Assignment (/content/week_1/assignment)
Assignment Hints (/content/week_1/hints)
Class Exercise (/content/week_1/lab)

Week 2
Week 3
Week 4
Week 5
Week 6

Using UIRefreshControl

Pull to refresh has been a standard UI convention in iOS since early days of Twitter. It's easy to include the
default pull to refresh control into any scroll view, including UIScrollView, UITableView, or UICollectionView.

Step 1: Create the UIRefreshControl


It's useful to create the UIRefreshControl as an instance variable at the top of the class because you need to
access it to stop the loading behavior.
class MyViewController: UIViewController {
var refreshControl: UIRefreshControl!
...

Step 2: Add the UIRefreshControl to the Scroll View


In viewDidLoad , add the refresh control as a subview of the scrollview. It's best to insert it at the lowest
index so that it appears behind all the views in the scrollview.

override func viewDidLoad() {


super.viewDidLoad()
refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: "onRefresh", forControlEvents: UIControlEv
ents.ValueChanged)
scrollView.insertSubview(refreshControl, atIndex: 0)
}

Step 3: Implement the onRefresh Method


In Step 2 above, we said whenever the refreshControl changes state, it should call the onRefresh method.
We need to define that method. For prototyping, you can simulate network loading by canceling refreshing
after a couple of seconds.
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
func onRefresh() {
delay(2, closure: {
self.refreshControl.endRefreshing()
})
}

You might also like