UIRefreshControl not refreshing when triggered pro

2019-07-06 21:30发布

I am trying to show a refresh control right when the view loads to show that I am getting getting data from Parse. The refresh control works as it should when the app is running, but I cannot get it to fire programmatically from anywhere in my app. This is the code that does not appear to run:

 override func viewDidAppear(animated: Bool) {
    self.refresher.beginRefreshing()
 }

Not only does this not run, but having the code in the app changes the attributes of the refresh control. When I have this code in the app, and show the refresher from user interaction, the refresh control does not have its attributed title as it usually does nor does it run the code that it should.

2条回答
可以哭但决不认输i
2楼-- · 2019-07-06 21:43

I needed to add a delay between setContentOffset and the self.refreshControl?.sendActions(for: .valueChanged) call. Without the delay, the refreshControl's attributedTitle would not render.

This works in iOS 10 (Swift 3):

self.tableView.setContentOffset(CGPoint(x:0, y:self.tableView.contentOffset.y - (self.refreshControl!.frame.size.height)), animated: true)

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: {
  self.refreshControl?.sendActions(for: .valueChanged)
})
查看更多
一夜七次
3楼-- · 2019-07-06 21:55

Here's an extension working on 10.3:

extension UIRefreshControl {
    func refreshManually() {
        if let scrollView = superview as? UIScrollView {
            scrollView.setContentOffset(CGPoint(x: 0, y: scrollView.contentOffset.y - frame.height), animated: false)
        }
        beginRefreshing()
        sendActions(for: .valueChanged)
    }
}
查看更多
登录 后发表回答