UIRefreshControl - Pull To Refresh in iOS 7
You do not have to explicitly set frame or start UIRefreshControl
. If it is a UITableView
or UICollectionView
, it should work like a charm by itself. You do need to stop it though.
Here is how you code should look like:
- (void)viewDidLoad {
[super viewDidLoad];
refreshControl = [[UIRefreshControl alloc]init];
[refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];
if (@available(iOS 10.0, *)) {
self.mytableView.refreshControl = refreshControl;
} else {
[self.mytableView addSubview:refreshControl];
}
}
In your refreshTable
function, you need to stop it when you are done refreshing your data. Here is how it is going to look like:
- (void)refreshTable {
//TODO: refresh your data
[refreshControl endRefreshing];
[self.mytableView reloadData];
}
Please note that if you are refreshing your data asynchronously then you need to move endRefreshing
and reloadData
calls to your completion handler.
Pull To Refresh in iOS 7
The 'UIActivityIndicator' that you are talking about is the new default appearance of a UIRefreshControl.
You pull down and as the circle completes it is showing how close to triggering a refresh you are.
UIRefreshControl with UICollectionView in iOS7
Having the same problem and found a workaround that seems to fix it.
This seems to be happening because the UIScrollView
is slowing down the tracking of the pan gesture when you pull past the edge of the scrollview. However, UIScrollView
is not accounting for changes to contentInset during tracking. UIRefreshControl
changes contentInset when it activates, and this change is causing the jump.
Overriding setContentInset
on your UICollectionView
and accounting for this case seems to help:
- (void)setContentInset:(UIEdgeInsets)contentInset {
if (self.tracking) {
CGFloat diff = contentInset.top - self.contentInset.top;
CGPoint translation = [self.panGestureRecognizer translationInView:self];
translation.y -= diff * 3.0 / 2.0;
[self.panGestureRecognizer setTranslation:translation inView:self];
}
[super setContentInset:contentInset];
}
Interestingly, UITableView
accounts for this by NOT slowing down tracking until you pull PAST the refresh control. However, I don't see a way that this behavior is exposed.
iOS7 UIRefreshControl changes contentInset
This is probably the reason why UIRefreshControl
is currently only supported on UITableViewController
, rather than by addition to any scrollview (which you can get away with, in many cases).
The refresh control does its magic by tinkering with the content insets of the scrollview - particularly when it ends refreshing. Unfortunately the view controller is also tinkering with the content insets of the scroll view to fit it under the translucent nav and status bars. Fun ensues. Is this also an issue on iOS 6 (or, "good old iOS6" as I called it when dealing with the same issue).
The quickest solution is probably to add your table view as a child UITableViewController instead of a simple subview. I think that UITableViewController manages the insets for you at the end of the refresh. If that doesn't work, I've got workarounds for this but it will have to wait until I get back in the office.
Pull to refresh default refresh level change
Since I do not think you can change the behaviour of how much distance you have to travel to initiate the refresh I would just trigger it manually when user scrolled enough, something like:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard !refreshControl.isRefreshing else {
return//do nothing if we are already refreshing
}
//set your threshold to whatever feels ok (I used -30 here)
if scrollView.contentOffset.y < -30 {
refreshTable()
refreshControl.beginRefreshing()
}
}
You also might have to play a bit with offsetting table view properly when refresh is active so that the UIActivityIndicator
is above your cells, and then adjust it again when you finish refreshing. Note you will have to call refreshControl.endRefreshing()
in refreshTable()
method once API
calls are completed or whatever you are doing there...
How To Apply Refresh Control To UITableView
use this UIRefreshControl
control :
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(refresh1:) forControlEvents:UIControlEventValueChanged];
[Delivered_TBL addSubview:refreshControl];
- (void)refresh1:(UIRefreshControl *)refreshControl
{
[self Service_Call];
[refreshControl endRefreshing];
}
How to use pull to refresh in Swift?
Pull to refresh is built in iOS. You could do this in swift like
let refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged)
tableView.addSubview(refreshControl) // not required when using UITableViewController
}
@objc func refresh(_ sender: AnyObject) {
// Code to refresh table view
}
At some point you could end refreshing.
refreshControl.endRefreshing()
How to activate refresh control without pulling down (Swift)
The solution was quite simple:
refreshControl.beginRefreshing()
https://developer.apple.com/documentation/uikit/uirefreshcontrol/1624842-beginrefreshing
Related Topics
Setting Contentoffset Programmatically Triggers Scrollviewdidscroll
Ios: Delete All Core Data Swift
How to Add a Footer to a Uitableview in Storyboard
iOS -- Detect the Color of a Pixel
How to Get Uiviewcontroller of a Uiview's Superview in iOS
Still Not Optimized for iPhone 6 and iPhone 6 Plus
Autolayout to Keep View Sizes Proportional
@Import "Unexpected '@' in Program"
Ios: Differencebetween -Init and -Viewload of a Viewcontroller
iOS Alamofire Stop All Requests
How to Determine Device Type from Swift? (Os X or iOS)
iOS Autolayout to Center My View Between Two Views
How to Use Avsamplebufferdisplaylayer in iOS 8 for Rtp H264 Streams with Gstreamer
iOS 11 Uisearchbar in Uinavigationbar
Text Color Based on Background Image
iOS 13 - How to Login in In-App Purchase Sandbox Account
How to Stop Uitableview from Clipping Uitableviewcell Contents in iOS 7