UITableViewHeaderFooterView in iOS 8

I have a UITableViewHeaderFooterView in which I change the textLabel font and 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: enter image description here

Here is how iOS 8 shows: enter image description here SetFont: doesn't seem to work here, or the 15pt font is bigger on iOS 8, which on iOS 7

This is how iOS 8 shows when I delete setFont: call enter image description here

As you can see, setFont does not affect the font, but it has the value textColor.

Am I missing something or is it a "beta bug" (I use simulators from the seeds of XCode6 GM, and I have the same problem on iPhone 5 with iOS 8 beta 5)?

Edit: The release of iOS 8 and Xcode 6.0.1 does not seem to fix the problem.

+8
fonts uitableview ios8 textcolor
source share
4 answers

Based on the answer to the tube, I created a subclass of UITableViewHeaderFooterView:

 @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 am now using the compabilityTextLabel property instead of textLabel . Note that the limit in the left space is automatically updated according to the insertion of the tableView delimiter. Feel free to comment / improve the code;)

-one
source share

[SWIFT version] This problem had a UITableView UITableViewStylePlain problem, that is, setting the header font in

 override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) {...} 

does not affect. Here is the code from my UITableViewController subclass that worked for me [verified with Xcode 6.4, iOS 8.4], see http://www.elicere.com/mobile/swift-blog-2-uitableview-section-header-color/

 override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { let header = view as? UITableViewHeaderFooterView //recast your view as a UITableViewHeaderFooterView if (header == nil) { return; } if (myHeaderFont != nil) { header!.textLabel.font = myHeaderFont; } } 

Heading height must be manually adjusted:

 override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { if (myHeaderFont == nil) { return 20; //DEFAULT_HEADER_HEIGHT_IN_POINTS; } return myHeaderFont.pointSize * 2; //HEIGHT_REL_TO_FONT; 

}

The rest was standard, but the completeness is shown here:

 override func viewDidLoad() { //... //https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewHeaderFooterView_class/index.html#//apple_ref/doc/uid/TP40012241 self.tableView.registerClass(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "HEADER_REUSE_ID") //... } override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { var header = tableView.dequeueReusableHeaderFooterViewWithIdentifier("HEADER_REUSE_ID") as? UITableViewHeaderFooterView; if (header == nil) { header = UITableViewHeaderFooterView(reuseIdentifier: "HEADER_REUSE_ID"); } header!.textLabel.text = myTitle; return header!; } 
+2
source share

I had the same problem and ended up subclassing UITableViewHeaderFooterView.

Here's a small reference implementation: just set header.headerLabel.text = @"myString"; .

 @interface BHRSectionHeaderView : UITableViewHeaderFooterView @property (nonatomic, strong) UILabel *headerLabel; @end @implementation BHRSectionHeaderView - (UILabel *)headerLabel { if (!_headerLabel) { _headerLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _headerLabel.font = [UIFont boldSystemFontOfSize:13.f]; _headerLabel.textColor = [UIColor redColor]; _headerLabel.translatesAutoresizingMaskIntoConstraints = NO; [self addSubview:_headerLabel]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(16)-[header]-(>=0)-|" options:0 metrics:nil views:@{ @"header": _headerLabel }]]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=0)-[header]-(>=0)-|" options:0 metrics:nil views:@{ @"header": _headerLabel }]]; [self addConstraint:[NSLayoutConstraint constraintWithItem:_headerLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]]; } return _headerLabel; } @end 
0
source share

You can change the font of textLabel by overriding the -layoutSubviews method:

 - (void)layoutSubviews { self.textLabel.font = [UIFont boldSystemFontOfSize:15.0f]; self.textLabel.textAlignment = NSTextAlignmentCenter; self.textLabel.textColor = [UIColor grayColor]; [super layoutSubviews]; } 
0
source share

All Articles