OK, I know that this has been asked previously, so please forgive me for asking again. It just seems like there has got to be an easier way to do this.
Is there a \
This is something I spent quite a while grappling with myself, only to find that there is no way to just change the tintColor of the section header. The solution I came up with was to screenshot the background of the section header, change the tint of it in Photoshop, and then use that as the background of the section header. Then its just a case of laying out the label.
As you said, the thing to use is the viewForHeaderInSection delegate method.
Here is what I've found works and looks like the Apple default:
UIView *customView = [[[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 320.0, 22.0)] autorelease];
customView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"headerbackground.png"]];;
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
headerLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
headerLabel.frame = CGRectMake(11,-11, 320.0, 44.0);
headerLabel.textAlignment = UITextAlignmentLeft;
headerLabel.text = @"Header Label";
[customView addSubview:headerLabel];
return customView;
Here "headerbackground.png" is a 1 pixel by 22 pixel (double that for iPhone 4) image that will get repeated across the length of the header.
Hope this helps!
we can change the header background color like this :
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
let tableHeader = view as! UITableViewHeaderFooterView
tableHeader.backgroundView?.backgroundColor = UIColor.white
}
If you change the appearance, like in the other answers, it is application wide. If you want to change just for a specific tableview, implement:
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
UITableViewHeaderFooterView * headerview = (UITableViewHeaderFooterView *)view;
headerview.contentView.backgroundColor = [UIColor greenColor];
headerview.textLabel.textColor = [UIColor whiteColor];
}
The UIAppearance method appearanceWhenContainedIn has been deprecated as of iOS 9.0; instead you can still do the exact same thing with the appearanceWhenContainedInInstancesOfClasses method and an array.
[UILabel appearanceWhenContainedInInstancesOfClasses:@[[UITableViewHeaderFooterView class]]].textColor = [UIColor darkTextColor];
With the newer UIAppearance way of doing things, in versions > iOS 6.0 you can just do, for example:
[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];
With UIAppearance you can change UILabel
text color on your UITableView
headers like this:
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setShadowColor:[UIColor whiteColor]];
Should work on iOS 6.0+. Can be called anywhere (viewDidLoad
etc).