iosswiftuitableviewuirefreshcontrol

What is the best way to hide UIRefreshControl iOS


I have trouble hiding refreshControl once a user leaves the ViewController while refreshControl is still visible. I have tried setting it removing it from superView (tableView), replacing it with new one, etc... The issue still remains with tableView when user returns to the screen, top content insets remain from refreshControl before and it leaves a white space on top of tableView and if I do not replace/hide refreshControl, it will be visible at this point.

Any suggestions?

Image: Transition between screens, refreshControl does not hide on viewDidDisappear


Solution

  • I have contacted a friend that gave a really nice answer. This was the code that helped me smoothly remove refreshControl in case it was stuck in frozen state on screen:

    func forceHideRefreshControl(tableView: UITableView) {
        if tableView.contentOffset.y < 0 { // Move tableView to top
            tableView.setContentOffset(CGPoint.zero, animated: true)
        }
    }
    

    Though if view controller hasn't finished loading, it won't have refreshControl visible. For that you'd need to call beginRefreshing() on it again, but I would do it with delay to avoid any animation problems. In any case, I think this was the best solution that actually removed the white spacing on top. I do not know why endRefreshing() did not work, but at least I found another way. Hope this helps anyone! :)

    NOTE: However, this solution is tested on stuck/frozen refreshControl only. I do not know what effect it will have if you do not have this problem, but still use this solution for hiding refreshControl.