Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 652
Imotep Avatar asked Sep 17 '14 10:09

Imotep


1 Answers

[SWIFT version] Just had the same problem in UITableView UITableViewStylePlain , i.e. Header font setting in

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

has no effect. Here is code from my subclass of UITableViewController what worked for me [tested 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;
    } 
  }

The header height needs to 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 showing here for completeness:

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!;
      }
like image 121
Dmitry Konovalov Avatar answered Nov 04 '22 10:11

Dmitry Konovalov