Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIRefreshControl is in wrong position in UITableViewController

I've seen quite a few problems with UIRefreshControl, and I'm having a problem as well with my UITableViewController. The problem occurs so randomly and henceforth I cannot figure out why or how it happens.

The problem is that sometimes when you scroll down on the tableView, the UIRefreshControl appears in the wrong place, and what seems like above/on top of the tableView itself. I'm attaching a screenshot of what the problem looks like, and also my code used to add the UIRefreshControl and it's refreshing method as well.

I appreciate any help offered!

enter image description here

- (void)viewDidLoad
{
    self.refreshControl = [[UIRefreshControl alloc] init];

    [self.refreshControl addTarget:self action:@selector(refreshing:) forControlEvents:UIControlEventValueChanged];

    [self.tableView addSubview:self.refreshControl];

    self.tableView.tableFooterView = [[UIView alloc] init];
}

- (void)refreshing:(UIRefreshControl*)refreshControl
{
    [refreshControl beginRefreshing];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    [refreshControl endRefreshing];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
 }
like image 636
klcjr89 Avatar asked Jan 24 '14 16:01

klcjr89


2 Answers

This is a known bug with iOS7; sometimes the refresh control is put incorrectly in the front of the view hierarchy instead of back. You can counter part of the problem by sending it to back after layout:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    [self.refreshControl.superview sendSubviewToBack:self.refreshControl];
}

Animation will still be imperfect, but at least it will still be below the table view. Please open a bug report with Apple for this issue.

Also, as stated in another answer, you should not add the refresh control to the view hierarchy yourself. The table view controller will do that for you. But that is not the issue here.

Swift version

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    refreshControl?.superview?.sendSubview(toBack: refreshControl!)
}
like image 66
Léo Natan Avatar answered Oct 17 '22 15:10

Léo Natan


I had the same problem and fixed it with this:

override func viewDidLoad() {
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: "refresh:", forControlEvents: .ValueChanged)
    tableView.backgroundView = refreshControl // <- THIS!!!
}

Instead of adding a subview assign the refreshControl as backgroundView

like image 18
Andres Avatar answered Oct 17 '22 16:10

Andres