`UIRefreshControl endRefreshing` not working

2019-07-14 18:57发布

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:

pull to refresh sticking

(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.

4条回答
霸刀☆藐视天下
2楼-- · 2019-07-14 19:29

For me setting UITableView contentOffset to zero, worked

In Swift 3.0

refreshControl.endRefreshing()
self.yourTableView.contentOffset = CGPoint.zero
查看更多
【Aperson】
3楼-- · 2019-07-14 19:32

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.

查看更多
在下西门庆
4楼-- · 2019-07-14 19:45

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()
    }
}
查看更多
趁早两清
5楼-- · 2019-07-14 19:47

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
})
查看更多
登录 后发表回答