问题
I've got a pull-to-refresh feature in my app that is, as far as I can tell, set up the "normal way":
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
[refreshControl addTarget:self action:@selector(refreshPage) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
The refreshing part works fine. After it is done refreshing I call
[self.refreshControl endRefreshing]
at which point, it appears to ignore that call and continues to show the pull-to-refresh "gap", only without the spinner:
(I do have a breakpoint at that line to verify that endRefreshing
IS actually being called)
If I jiggle the page with my thumb (pull it down slightly without pulling it far enough to trigger another pull-to-refresh), it will fix itself and spring back into place. But why doesn't it spring back when I call [self.refreshControl endRefreshing]
? Is there a way I can programmatically force it to spring back?
I've also tried placing the endRefreshing
call in a delay:
[self.refreshControl performSelector:@selector(endRefreshing) withObject:nil afterDelay:0.0]
but it still ignores the call.
回答1:
You could stop the refreshControl
and reset the tableview
position with an animation
self.refreshControl.endRefreshing()
UIView.animate(withDuration: 0.5, animations: {
yourTableview.contentOffset = CGPoint.zero
})
回答2:
It turns out, in my case, the problem was that the code for adding the UIRefreshControl
was being hit more than once and creating more than one refreshControl, so endRefreshing
was being called on a different instance of UIRefreshControl
than the one that started the pull-to-refresh.
Dumb mistake on my part.
回答3:
For me setting UITableView contentOffset
to zero, worked
In Swift 3.0
refreshControl.endRefreshing()
self.yourTableView.contentOffset = CGPoint.zero
回答4:
After call refreshControl.endRefreshing()
property isRefreshing = false
, but it can still be presented. Problem is in layers - while other animation is in progress or view controller with refresh control is not visible (other vc is presented or key window changed to another one). The code below helped me to fix this problem
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
guard let strongSelf = self else { return }
if strongSelf.refreshControl.isRefreshing {
strongSelf.refreshControl.endRefreshing()
} else if !strongSelf.refreshControl.isHidden {
strongSelf.refreshControl.beginRefreshing()
strongSelf.refreshControl.endRefreshing()
}
}
来源:https://stackoverflow.com/questions/36727188/uirefreshcontrol-endrefreshing-not-working