Search code examples
uitableviewfontsios8textcolor

UITableViewHeaderFooterView in iOS 8


I have a UITableViewHeaderFooterView in which I change the textLabel font and the background color

UITableViewHeaderFooterView* header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
if(!header)
{
    header = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"header"];
    [header.textLabel setFont:[UIFont boldSystemFontOfSize:15]];
    [header.contentView setBackgroundColor:[UIColor colorWithRed:213/255.0 green:213/255.0 blue:213/255.0 alpha:1]];
}

Here is how iOS 7 shows it: enter image description here

Here is how iOS 8 shows it: enter image description here The setFont: doesn't seems to take effect here, or the 15pt font is bigger on iOS 8 that on iOS 7

Here is how iOS 8 shows it when I remove the setFont: call enter image description here

As you can see, setFont has no effect on the font, but it has on the textColor.

Am I missing something or those are "beta bugs" (I'm using simulators from XCode6 GM seed, and I have the same issue on a iPhone 5 with iOS 8 beta 5) ?

Edit: iOS 8 release and XCode 6.0.1 doesn't seems to fix the problem


Solution

  • Based on tubtub's answer, I made a UITableViewHeaderFooterView subclass:

    @interface CompatibilityTableViewHeaderFooterView : UITableViewHeaderFooterView
    @property (nonatomic,readonly) UILabel* compabilityTextLabel;
    @end
    
    @implementation CompatibilityTableViewHeaderFooterView
    {
        UILabel* iOS8TextLabel;
        NSLayoutConstraint* iOS8TextLabelLeftMarginConstraint;
    }
    
    -(instancetype) initWithReuseIdentifier:(NSString *)reuseIdentifier
    {
        if (self = [super initWithReuseIdentifier:reuseIdentifier])
        {
            self.contentView.backgroundColor = GRIS_213;
            self.compabilityTextLabel.font = [UIFont boldSystemFontOfSize:15];
        }
        return self;
    }
    
    -(UILabel*) compabilityTextLabel
    {
        if ([UIDevice currentDevice].systemVersion.floatValue < 8.0)
        {
            return self.textLabel;
        }
        else
        {
            if (!iOS8TextLabel)
            {
                iOS8TextLabel = [[UILabel alloc] init];
                iOS8TextLabel.translatesAutoresizingMaskIntoConstraints = NO;
    
                [self addSubview:iOS8TextLabel];
    
                iOS8TextLabelLeftMarginConstraint = [NSLayoutConstraint constraintWithItem:iOS8TextLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
    
                [self addConstraint:iOS8TextLabelLeftMarginConstraint];
                [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[iOS8TextLabel]-(>=0)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(iOS8TextLabel)]];
                [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[iOS8TextLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(iOS8TextLabel)]];
            }
            return iOS8TextLabel;
        }
    }
    
    -(UITableView*) searchForTableView
    {
        UIView* currentView = self;
        while (currentView)
        {
            currentView = currentView.superview;
            if ([currentView isKindOfClass:[UITableView class]])
            {
                return (UITableView*)currentView;
            }
        }
        return nil;
    }
    
    -(void) layoutSubviews
    {
        [super layoutSubviews];
    
        UITableView* tableView = [self searchForTableView];
        if (tableView)
        {
            iOS8TextLabelLeftMarginConstraint.constant = tableView.separatorInset.left;
        }
    }
    

    So basically I now only use the compabilityTextLabelproperty instead of textLabel. Note that the left space constraint is automatically updated to match the tableView separator inset. Feel free to comment/improve my code ;)