Give pull-to-refresh & infinite scrolling to any UIScrollView with 1 line of code.
These UIScrollView categories makes it super easy to add pull-to-refresh and infinite scrolling fonctionalities to any UIScrollView (or any of its subclass). Instead of relying on delegates and/or subclassing
UIViewController, SVPullToRefresh uses the Objective-C runtime to add the following 3 methods to
UIScrollView:
- (void)addPullToRefreshWithActionHandler:(void (^)(void))actionHandler; - (void)addPullToRefreshWithActionHandler:(void (^)(void))actionHandler position:(SVPullToRefreshPosition)position; - (void)addInfiniteScrollingWithActionHandler:(void (^)(void))actionHandler;
Add
pod 'SVPullToRefresh'to your Podfile or
pod 'SVPullToRefresh', :headif you're feeling adventurous.
Important note if your project doesn't use ARC: you must add the
-fobjc-arccompiler flag to
UIScrollView+SVPullToRefresh.mand
UIScrollView+SVInfiniteScrolling.min Target Settings > Build Phases > Compile Sources.
SVPullToRefresh/SVPullToRefreshfolder into your project.
UIScrollView+SVPullToRefresh.hand/or
UIScrollView+SVInfiniteScrolling.h
(see sample Xcode project in
/Demo)
[tableView addPullToRefreshWithActionHandler:^{ // prepend data to dataSource, insert cells at top of table view // call [tableView.pullToRefreshView stopAnimating] when done }];
or if you want pull to refresh from the bottom
[tableView addPullToRefreshWithActionHandler:^{ // prepend data to dataSource, insert cells at top of table view // call [tableView.pullToRefreshView stopAnimating] when done } position:SVPullToRefreshPositionBottom];
If you’d like to programmatically trigger the refresh (for instance in
viewDidAppear:), you can do so with:
[tableView triggerPullToRefresh];
You can temporarily hide the pull to refresh view by setting the
showsPullToRefreshproperty:
tableView.showsPullToRefresh = NO;
The pull to refresh view can be customized using the following properties/methods:
@property (nonatomic, strong) UIColor *arrowColor; @property (nonatomic, strong) UIColor *textColor; @property (nonatomic, readwrite) UIActivityIndicatorViewStyle activityIndicatorViewStyle;
You can access these properties through your scroll view's
pullToRefreshViewproperty.
For instance, you would set the
arrowColorproperty using:
tableView.pullToRefreshView.arrowColor = [UIColor whiteColor];
[tableView addInfiniteScrollingWithActionHandler:^{ // append data to data source, insert new cells at the end of table view // call [tableView.infiniteScrollingView stopAnimating] when done }];
If you’d like to programmatically trigger the loading (for instance in
viewDidAppear:), you can do so with:
[tableView triggerInfiniteScrolling];
You can temporarily hide the infinite scrolling view by setting the
showsInfiniteScrollingproperty:
tableView.showsInfiniteScrolling = NO;
The infinite scrolling view can be customized using the following methods:
- (void)setActivityIndicatorViewStyle:(UIActivityIndicatorViewStyle)activityIndicatorViewStyle; - (void)setCustomView:(UIView *)view forState:(SVInfiniteScrollingState)state;
You can access these properties through your scroll view's
infiniteScrollingViewproperty.
SVPullToRefresh extends
UIScrollViewby adding new public methods as well as a dynamic properties.
It uses key-value observing to track the scrollView's
contentOffset.
SVPullToRefresh is brought to you by Sam Vermette and contributors to the project. If you have feature suggestions or bug reports, feel free to help out by sending pull requests or by creating new issues. If you're using SVPullToRefresh in your project, attribution would be nice.
Big thanks to @seb_morel for his Demistifying the Objective-C runtime talk which really helped for this project.
Hat tip to Loren Brichter for inventing pull-to-refresh.