5

I have a UITableViewController that will display a table view header (NOT section header) if certain criteria is met. Displaying the controller with or without the header view on load works just fine. But if I show the header, then remove it, the spacing (where the header's frame was) remains at the top of the table view every time.

I have tried every combination of the following when removing the table view header with no luck:

[self.tableView beginUpdates];
self.tableView.tableHeaderView = nil;
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
[self.tableView reloadData];
[self.tableView endUpdates];

Anyone else encountered this?

Table View Header code:

- (void)updateTableHeaderView
{
    if (self.alerts.count && self.isViewLoaded) {
        [self.tableView beginUpdates];

        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.bounds), 40.0f)];
        headerView.preservesSuperviewLayoutMargins = YES;
        self.tableView.tableHeaderView = headerView;

        UIButton *alertButton = [UIButton buttonWithType:UIButtonTypeCustom];
        alertButton.tintColor = [UIColor whiteColor];
        alertButton.translatesAutoresizingMaskIntoConstraints = NO;
        [alertButton setBackgroundImage:[UIImage imageWithColor:alertTintColor] forState:UIControlStateNormal];
        [alertButton setBackgroundImage:[UIImage imageWithColor:alertHighlightedTintColor] forState:UIControlStateHighlighted];
        [alertButton addTarget:self action:@selector(alertButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:alertButton];

        NSDictionary *views = NSDictionaryOfVariableBindings(alertButton);
        NSDictionary *metrics = nil;

        [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[alertButton]|" options:0 metrics:metrics views:views]];
        [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[alertButton]|" options:0 metrics:metrics views:views]];

        [self.tableView endUpdates];
    }
    else if (self.isViewLoaded) {
        [self.tableView beginUpdates];
        self.tableView.tableHeaderView = nil;
        [self.tableView endUpdates];
    }
}
JimmyJammed
  • 9,598
  • 19
  • 79
  • 146
  • works fine, i just try it !! can you explain more please ? – NSSakly Jul 20 '16 at 17:46
  • @NSSakly I added the code that generates (and removes) the table view header as needed. The problem is that if I show the header (when a user has alerts) then they view them, I want to remove the header. But when I remove it, it leaves the space where the header was (i.e. a gap between top of table and first section/row). – JimmyJammed Jul 20 '16 at 17:56

2 Answers2

8

It appears this is an iOS bug, and this is the current workaround for removing a tableHeaderView and it's spacing until fixed:

    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.bounds), 1.0f)];
JimmyJammed
  • 9,598
  • 19
  • 79
  • 146
-3

Please see if you handle it also in:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
manta
  • 369
  • 1
  • 9
  • As stated in the OP, this issue is with the table view header, NOT a section header (i.e. self.tableView.tableHeaderView). The heightForHeaderInSection protocol method applies only to section headers. – JimmyJammed Jul 20 '16 at 18:16