I've got a simple app that have no need to be constructed with UITableViewController. Is there any chance to create 'pull to refresh' functionality for the simple UIViewController?
Yes, there is. In fact, in order to use UIRefreshControl
there is only one requirement - you can place UIRefreshControl
inside of UIScrollView
or its subclass (for example UITableView
or UICollectionView
).
Example code in Swift:
override func viewDidLoad() {
{
super.viewDidLoad()
//...
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(didPullToRefresh), forControlEvents: .ValueChanged)
scrollView.addSubview(refreshControl)
}
func didPullToRefresh() {
}
Example code in Objective-c:
- (void)viewDidLoad
{
[super viewDidLoad];
//...
UIRefreshControl *refreshControl = [UIRefreshControl new];
[refreshControl addTarget:self action:@selector(didPullToRefresh) forControlEvents:UIControlEventValueChanged];
[self.scrollView addSubview:refreshControl];
}
- (void)didPullToRefresh
{
}
As of iOS 6 there is a refreshControl
property on UIScrollView
(and hence it's subclasses, UITableView
and UICollectionView
).
Code from the Apple documentation: https://developer.apple.com/documentation/uikit/uirefreshcontrol#//apple_ref/occ/instm/UIRefreshControl
func configureRefreshControl () {
// Add the refresh control to your UIScrollView object.
myScrollingView.refreshControl = UIRefreshControl()
myScrollingView.refreshControl?.addTarget(self, action:
#selector(handleRefreshControl),
for: .valueChanged)
}
@objc func handleRefreshControl() {
// Update your content…
// Dismiss the refresh control.
DispatchQueue.main.async {
self.myScrollingView.refreshControl?.endRefreshing()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With