Refresh Control on top of tableview when I go back to view controller

11,669

Solution 1

I found the fix here: https://stackoverflow.com/a/29088409/209200

Instead of self.tableView.addSubview(refreshControl!), it should be self.tableView.insertSubview(refreshControl!, atIndex: 0).

That was causing the refres control to appear over the tableview.

Solution 2

Try to set z index as -1 which means UIRefreshControl is behind any view.

refreshControl.layer.zPosition = -1

and

tableView.refreshControl?.beginRefreshing()
self.tableView.refreshControl?.endRefreshing()

Both must use in pair.

Solution 3

Try this,

The Above solutions are fine but tableView.refreshControl is available for UITableViewController only, till iOS 9.x and comes in UITableView from iOS 10.x onwards.

To fix this Just use :

Written in Swift 3 -

let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(FeedViewController.loadNewData), for: UIControlEvents.valueChanged)
// Fix for the RefreshControl Not appearing in background
tableView?.addSubview(refreshControl)
tableView.sendSubview(toBack: refreshControl)

Solution 4

Inserting the refresh control at index 0 doesn't work, just set it as the tableview's background view:

tableView.backgroundView = refreshControl
Share:
11,669

Related videos on Youtube

jplozano
Author by

jplozano

Updated on June 04, 2022

Comments

  • jplozano
    jplozano almost 2 years

    For some reason, the first time the view controller is loaded, the refresh control behaves as expected (that it, showing under tableview when swipe down). The problem is when I tap on another tab, and then go back to the view controller, the refresh control does not fade in anymore and is visible on top of the tableview.

    When I go back to view controller

    This is part of the code:

    class CoreTableViewController : UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    var tableView:UITableView!
    var tableData:Array<AnyObject> = []
    var dataFetched:Bool = false
    var refreshControl:UIRefreshControl?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.edgesForExtendedLayout = UIRectEdge.None;
    
        self.tableView = self.assignTableView()
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None;
    
        if self.enableRefresher() {
            self.setRefreshControl()
        }
    }
    
    func enableRefresher() -> Bool {
        return true
    }
    
    func fetchData(cleanFirst:Bool = false) {
        // Fetch data.
    }
    
    func refresh() {
        self.fetchData(true)
        self.refreshControl!.endRefreshing()
    }
    
    func setRefreshControl() {
        self.refreshControl = UIRefreshControl()
        //self.refreshControl!.attributedTitle = NSAttributedString(string: "Actualizar")
        self.refreshControl!.addTarget(self, action: #selector(CoreTableViewController.refresh), forControlEvents: UIControlEvents.ValueChanged)
        self.tableView.addSubview(refreshControl!)
    }
    
    }
    

    Any thoughts?

  • Ricardo
    Ricardo about 7 years
    What about if you want to use a collection view? I have exactly the same problem with the refresh control inside a collection view.
  • Dima Deplov
    Dima Deplov about 7 years
    @Ricardo, check out collection view documentation. Looks like UIRefreshControl is made exclusively for UITableView. Maybe you need to browse for already implemented solution or made it by yourself... Anyway it's a topic for different question.
  • Alexey Strakh
    Alexey Strakh over 6 years
    even with this, the refreshesControl is shown above the table starting ios 11.x (ios 9 works fine)
  • bpolat
    bpolat about 6 years
    There is a simulator bug on iOS 11 but device works okay.
  • Nicolai Harbo
    Nicolai Harbo over 3 years
    Brilliant! This is what I was looking for - thanks!
  • xlsmearlx
    xlsmearlx over 2 years
    this did the trick for me, thanks!