What is the best way to hide UIRefreshControl iOS

1.6k Views Asked by At

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

5

There are 5 best solutions below

0
On BEST ANSWER

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.

4
On

When you present the new screen just use .endRefreshing() on your refreshControl.

5
On

Try this one :-

refreshControl.tintColor = .clear
0
On

Initialize the refresh control:

lazy var refreshControl: UIRefreshControl = {
        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: 
                     #selector(ViewController.handleRefresh(_:)), 
                     for: UIControlEvents.valueChanged)
        refreshControl.tintColor = UIColor.red

        return refreshControl
    }()

Handle the refresh and end the refreshing:

func handleRefresh(_ refreshControl: UIRefreshControl) {
        self.tableView.reloadData()
        refreshControl.endRefreshing()
    }

Add the refresh control:

self.tableView.addSubview(self.refreshControl)
0
On

I've made it working for me by doing the following, On ViewdidLoad I've made the

override func viewDidLoad() {
    refreshControl.tintColor = .clear
}

and When calling the selector function I've used refreshControl.endRefreshing() before calling my custom Loading function.

It's not showing the loading indicator for me.