`UIRefreshControl endRefreshing` not working

两盒软妹~` 提交于 2020-01-04 05:34:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!