Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the style of UITableView section headers

Tags:

I'm working with iOS 5 and have dynamically generated cells in a table (2 sections of 3 rows each). Each section has a header that is also dynamically generated using the titleForHeaderInSection call.

I also have an image set as the background for the table that makes the default color of the section headers hard to read. I haven't found a way to change the color of the section headers (or shadow color, font, text size, etc for that matter) either through the Storyboard interface or programmatically! Please help!

like image 961
Russell Winkler Avatar asked Mar 04 '12 16:03

Russell Winkler


People also ask

How do I scroll the header along with UITableView?

If you want to have table view header along with section headers you have to set UITableViewStyle property to UITableViewStyleGrouped . Show activity on this post. If you have a single header in the table then you can use tableHeaderView as below: tableView.


2 Answers

This also works in iOS5+. It applies to all of the section headers and footers in the tableview and suited my needs.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont fontWithName:@"MyFont" size:8]];
}

Dan

like image 127
Dan Power Avatar answered Sep 22 '22 16:09

Dan Power


You can modify the font size/color/etc by creating your own view for the section header using the method tableView:viewForHeaderInSection:

There's an example of this technique here

like image 40
jonkroll Avatar answered Sep 20 '22 16:09

jonkroll