Tomaž Ravljen
Tomaž Ravljen

Reputation: 1

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

Upvotes: 0

Views: 1772

Answers (5)

Abdullah All Abir
Abdullah All Abir

Reputation: 1

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.

Upvotes: 0

Tomaž Ravljen
Tomaž Ravljen

Reputation: 1

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.

Upvotes: 0

Saurabh Jain
Saurabh Jain

Reputation: 1698

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)

Upvotes: 1

Shah Nilay
Shah Nilay

Reputation: 798

Try this one :-

refreshControl.tintColor = .clear

Upvotes: 3

Alex Ioja-Yang
Alex Ioja-Yang

Reputation: 1533

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

Upvotes: 0

Related Questions