UITableViewHeaderFooterView: unable to change background color

I am trying to change the background color of a UITableViewHeaderFooterView. Although the view appears, the background color remains the default color. I get a log from xcode saying:

Setting the background color in a UITableViewHeaderFooterView has been deprecated. Use contentView.backgroundColor instead.

However, none of the following options work:

myTableViewHeaderFooterView.contentView.backgroundColor = [UIColor blackColor]; myTableViewHeaderFooterView.backgroundView.backgroundColor = [UIColor blackColor]; myTableViewHeaderFooterView.backgroundColor = [UIColor blackColor]; 

I also tried changing the background color of the view in the xib file.

Any suggestions? Thank.

+110
ios iphone xcode uitableview
Mar 24 '13 at 22:37
source share
15 answers

You must either use myTableViewHeaderFooterView.tintColor, or assign a custom background view to myTableViewHeaderFooterView.backgroundView.

+77
Mar 24 '13 at 22:43
source share

iOS 8, 9, 10 ...

The only way to set any color (with any alpha) is to use backgroundView :

Swift 3

 self.backgroundView = UIView(frame: self.bounds) self.backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5) 

Obj-c

 self.backgroundView = ({ UIView * view = [[UIView alloc] initWithFrame:self.bounds]; view.backgroundColor = [UIColor colorWithWhite: 0.5 alpha:0.5]; view; }); 



Comments replies

  • None of these other features work reliably (despite the comments below)

     // self.contentView.backgroundColor = [UIColor clearColor]; // self.backgroundColor = [UIColor clearColor]; // self.tintColor = [UIColor clearColor]; 
  • backgroundView resizes automatically. (No need to add restrictions)

  • Control alpha with UIColor(white: 0.5, alpha: 0.5) or backgroundView.alpha = 0.5 .
    (of course, any color will do)

  • When using XIB, create the root view a UITableViewHeaderFooterView and programmatically connect the backgroundView :

    Register:

     tableView.register(UINib(nibName: "View", bundle: nil), forHeaderFooterViewReuseIdentifier: "header") 

    Download with:

     override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { if let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") { let backgroundView = UIView(frame: header.bounds) backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5) header.backgroundView = backgroundView return header } return nil } 



Demo

► Find this solution on GitHub and more on Swift Recipes .

+179
Aug 31 '14 at 4:00
source share

In iOS 7, contentView.backgroundColor worked for me, tintColor did not.

  headerView.contentView.backgroundColor = [UIColor blackColor]; 

Although clearColor did not work for me, the solution I found was to set the backgroundView property to a transparent image. Maybe this will help someone:

 UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0); UIImage *blank = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); headerView.backgroundView = [[UIImageView alloc] initWithImage:blank]; 
+21
Apr 30 '14 at 14:38
source share

Make sure you set the backgroundColor contentView for the UITableViewHeaderFooterView :

 self.contentView.backgroundColor = [UIColor whiteColor]; 

Then it will work.

+12
Dec 15 '14 at 10:05
source share

For me, I tried everything that was said above, but still got the warning "Setting the background color in the UITableViewHeaderFooterView is out of date. Use contentView.backgroundColor instead." then I tried this: in the xib file, the background color for the header was selected to clear the color instead of the default value, as soon as I changed it to the default value, the warning disappeared. it was

Modified for this

+11
Jun 19 '17 at 17:12
source share

For clear color I use

 self.contentView.backgroundColor = [UIColor clearColor]; self.backgroundView = [UIView new]; self.backgroundView.backgroundColor = [UIColor clearColor]; 

It seems to me that this is good.

+6
Jan 12 '15 at 6:06
source share

In iOS9, headerView.backgroundView.backgroundColor worked for me:

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ TableViewHeader *headerView = (TableViewHeader *)[super tableView:tableView viewForHeaderInSection:section]; headerView.backgroundView.backgroundColor = [UIColor redColor]; } 

In iOS8, I used headerView.contentView.backgroundColor without any problems, but now with iOS 9 I had a strange problem due to which the background color did not fill the entire cell space. So I tried just headerView.backgroundColor and I got the same error from OP.

Setting the background color in the UITableViewHeaderFooterView was deprecated. Use contentView.backgroundColor instead.

So now everything is working fine and without warning using headerView.backgroundView.backgroundColor

+4
06 Aug '15 at 16:41
source share

If you are customizing the section header cell using Storyboard / Nib , make sure that the default background color for the Table Header view is.

And if you are a subclass of UITableViewHeaderFooterView and using nib, then you need to create an IBOutlet to represent the content and name it, for example. containerView . This should not be confused with the contentView , which is the parent of this container view.

With this setting, you change the background color of the containerView instead.

+4
Nov 07 '15 at 11:04
source share

if you created a custom subclass of UITableViewHeaderFooterView with the xib file, then you must override setBackgroundColor . Keep it empty.

 -(void)setBackgroundColor:(UIColor *)backgroundColor { } 

And that will solve your problem.

+4
Dec 27 '15 at 18:00
source share

I tried the appearance chain on the net, and it worked for me.

 [[UIView appearanceWhenContainedIn:[UITableViewHeaderFooterView class], [UITableView class], nil] setBackgroundColor: [UIColor.grayColor]]; 
+1
Aug 14 '14 at 23:13
source share

perhaps because backgroundView does not exist

 override func draw(_ rect: CGRect){ // Drawing code let view = UIView() view.frame = rect self.backgroundView = view self.backgroundView?.backgroundColor = UIColor.yourColorHere } 

which work for me.

+1
Dec 17 '17 at 9:25
source share

A clear color background setting is great for visible headers. If the table scrolls to display the headings below, this decision is not executed.

The PSMy table consists only of headers without any cells.

0
Sep 22 '15 at 6:41
source share

Swift:

 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView { var headerView: TableViewHeader = super.tableView(tableView, viewForHeaderInSection: section) as! TableViewHeader headerView.backgroundView.backgroundColor = UIColor.redColor() } 
0
Feb 11 '16 at 9:10
source share

Create a UIView and set the background color, then set it to self.backgroundView.

 - (void)setupBackgroundColor:(UIColor *) color { UIView *bgView = [[UIView alloc] initWithFrame:self.bounds]; bgView.backgroundColor = color; self.backgroundView = bgView; } 
0
Jun 07 '16 at 10:11
source share

I have to share my experience. I had this piece of code that worked great with iOS 10 and iOS 11 headerView?.contentView.backgroundColor = .lightGray

Then I suddenly decided to deploy the application for iOS 9, as there are some devices (iPad mini, some older generation does not update any OS for 9). The only solution that worked for all iOS 9, 10, and 11 was to determine the base type of the header, which then contains all the other headings, plug it from the storyboard, and set the backgroundColor this base view.

You want to be careful when connecting the outlet, so as not to call it: backgroundView , since some superclass already has a property with this name. I called my containingView

Also, when connecting an output controller, click on the view in the Document Outline to make sure that it is not connected to the file owner

0
Nov 03 '17 at 9:30
source share



All Articles