In iOS 5 and below, to customize the background color and text color of a UITableView’s header or footer, we needed to create a custom UIView then return it from the following delegate methods:
1 2 |
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section |
In iOS 6 and iOS 7, as well as iOS 8, we do not need a custom UIView. We can now set the colors (and possibly other attributes) using the new APIs. Refer to the code below to see how to do it.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { // Set the text color of our header/footer text. UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view; [header.textLabel setTextColor:[UIColor whiteColor]]; // Set the background color of our header/footer. header.contentView.backgroundColor = [UIColor blackColor]; // You can also do this to set the background color of our header/footer, // but the gradients/other effects will be retained. // view.tintColor = [UIColor blackColor]; } |
Very good article. I am facing some of these issues as well..
Thank you! 🙂
Hope this article helped you.
Thanks for that! Helped a lot.
Where do I need to add this code exactly?
Hi @Tofo,
You can put this code in the implementation code of the delegate of your UITableView.
Usually, you will put this code in a UIViewController which houses your UITableView.
Or, if you are using a subclass of UITableViewController, you can put this code in that subclass’ implementation file (e.g. MasterViewController.m).
Thanks man, so far best solution in 2015
It’s best , Thank you sooo much…….. Dj Sison