Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the color of UITableView section headers

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 'simple' way to change the UITableView section header background color? I know that I can use the delegate method 'viewForHeaderInSection' to hand the UITableView a custom UIView to use for the section headers, but all I really want to do is set the 'tintColor'. There is a tintColor property on the search bar, why not for the section headers.

If I use viewForHeaderInSection to create my own custom UIView, I have to create my own label as well. I have to style and position my label to look just like the rest of the Apple standard. I have to style and size the 'background' to look like the rest of the Apple standard.

I can't find a way to simply ask for the section header and then change it's color. Am I missing something or do I really have to do this the hard way.

If I have to do this the hard way, does someone have all of the styling information that matches the Apple standard? - bar is 'translucent' - bar has some shading on the top and the bottom - label has a certain size, position, shadings, etc

Thanks. And once again, sorry for asking this question again.

like image 665
toofah Avatar asked Jul 20 '10 22:07

toofah


People also ask

How do I change the header color in Swift?

user1639164 ,you can use header. backgroundView. backgroundColor=[UIColor blackColor]; to set the tint for the header background.

How do I change the background color of a table in Swift?

Basic Swift Code for iOS Apps For changing the background color of the table view cell, you should change the contentView. backgroundColor property of the cell. Now run the project to see the effect.

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


2 Answers

With the newer UIAppearance way of doing things, in versions > iOS 6.0 you can just do, for example:

[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];
like image 138
Mason G. Zhwiti Avatar answered Oct 02 '22 18:10

Mason G. Zhwiti


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).

like image 29
Sokolov Avatar answered Oct 02 '22 17:10

Sokolov