UITableView's section headers are less identifiable in iOS 7. Are there any way to set the UITableView section header color in iOS 7 directly rather creating a new UIView.
Note: I found some solutions by creating a new UIView in,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
but I really wanted to keep the Apple's properties except color. Are there any way to do it without this method.
Set the background and text color of section area: (Thanks to William Jockusch and Dj S) To change the background color, text label color and font for the Header View of a UITableView Section, simply override willDisplayHeaderView for your table view like so:
For example, the Settings app uses tables and a navigation controller to organize the system settings. UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content.
Basically, to add sections and an index list in the UITableView, you need to deal with these methods as defined in UITableViewDataSource protocol: numberOfSectionsInTableView: method – returns the total number of sections in the table view. Usually we set the number of section to 1.
There are two ways that we can add the header and footer, one way is to add it in using Interface Builder and the other way is to do it with code. In this tutorial we will be using both Interface Builder as well as code. The first thing that we need to do is to set up the UITableView and get it to show some data.
Implement tableView:willDisplayHeaderView:forSection:
and update the view that you are passed.
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *v = (UITableViewHeaderFooterView *)view;
v.backgroundView.backgroundColor = [UIColor darkGrayColor];
}
(assuming that you supplied a UITableViewHeaderFooterView
instance in your implementation for tableView:viewForHeaderInSection:
)
In the case of Swift 3 the way is to achieve it via UITableViewDelegate method:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.backgroundColor = UIColor.gray
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With