Centering textLabel vertically in UITableViewHeaderFooterView from tableView: willDisplayHeaderView: forSection:

ios, iphone, objective-c, uitableview

Solution

Well I ended up figuring out how to do this. Unfortunately you cannot handle vertical centering in the tableView:willDisplayHeader: function because the tableViewHeaderFooterView does some tricky tableView behind the scenes stuff. Therefore I found the best way is to return a label in your own header view. The relevent code is here:

- (UIView*) tableView: (UITableView*) tableView viewForHeaderInSection: (NSInteger)  section
{
    UIView* view = [[UIView alloc] init];
    UILabel* label = [[UILabel alloc] init];

    label.text = [self tableView: tableView titleForHeaderInSection: section];
    label.textAlignment = NSTextAlignmentCenter;

    [label sizeToFit];
    label.translatesAutoresizingMaskIntoConstraints = NO;

    [view addSubview:label];

    [view addConstraints:
     @[[NSLayoutConstraint constraintWithItem:label
                                  attribute:NSLayoutAttributeCenterX
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:view
                                  attribute:NSLayoutAttributeCenterX
                                 multiplier:1 constant:0],
     [NSLayoutConstraint constraintWithItem:label
                                  attribute:NSLayoutAttributeCenterY
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:view
                                  attribute:NSLayoutAttributeCenterY
                                 multiplier:1 constant:0]]];

    return view;
}

Hope this will help someone else out!

Cheers

Problem

I am trying to vertically center the text label for a tableViewHeader. Here is the relevant code from `- (void) tableView: (UITableView*) tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section:` ``` - (void) tableView: (UITableView*) tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { if(![view isKindOfClass:[UITableViewHeaderFooterView class]]){ return; } UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view; UILabel* label = tableViewHeaderFooterView.textLabel; label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont rev_lightAppFontOfSize: 24.0f]; label.textColor = [UIColor rev_colorWithHex: 0x858585]; label.text = [label.text capitalizedString]; label.center = tableViewHeaderFooterView.center; // label not in superview yet tableViewHeaderFooterView.layer.borderColor = [UIColor greenColor].CGColor; tableViewHeaderFooterView.layer.borderWidth = 2.0f; tableViewHeaderFooterView.contentView.layer.borderColor = [UIColor purpleColor].CGColor; tableViewHeaderFooterView.contentView.layer.borderWidth = 2.0f; label.layer.borderColor = [UIColor orangeColor].CGColor; label.layer.borderWidth = 2.0f; } ``` the label object is not a subview of tableViewHeaderFooterView so the centering code does not seem to be working. Any thoughts?

Original source