I'm creating a UITableViewHeaderFooterView from a xib file, and almost everything is working properly.
The problem is that now when I try to change the background color (or if I had one configured in the xib), it will constantly output this message to the console:
Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead.
This means I have two problems:
contentView.backgroundColor
suggestion, but when I try to follow that suggestion, nothing happens. (This is because contentView
is nil
.)Note: There is a similar question here, but that was mainly concerned with muting the message, not finding an alternative solution that resolves both problems above.
Update: To be clear, I want to continue using a xib file for the header view, and want to be able to call dequeueReusableHeaderFooterViewWithIdentifier: so that the table can be efficient in its management of the views.
Here is the best way I've found to solve this:
UITableViewHeaderFooterView
to Default.UITableViewHeaderFooterView
, and call it Content View. (This is exactly what Apple does with a UITableViewCell
, and we are just mimicking that structure.)inside
the Content View
.contentView
property in an extension method, and add IBOutlet
to its definition. (See code below.)contentView.backgroundColor
in code, just as the error message tells you to..h file:
@interface ABCHeaderView : UITableViewHeaderFooterView
@end
.m file:
@interface ABCHeaderView ()
@property (nonatomic, readwrite, retain) IBOutlet UIView *contentView;
@end
@implementation ABCHeaderView
@synthesize contentView;
@end
This hierarchy is consistent with Apple's documentation:
If you have custom content to display, create the subviews for your content and add them to the view in the contentView property.
To get rid of the warning you must not set the background color. The problem is when you creating TableHeader in such way, IB does not provide special object for that. Removing background color in source code will excelent solve described problem, just in some clicks.
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
Now you can change background like:
override func awakeFromNib() {
backgroundView = UIView()
backgroundView?.backgroundColor = UIColor.whiteColor()
}
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