UITableViewHeaderFooterView: Unable to change background color

前端 未结 20 2222
误落风尘
误落风尘 2020-12-23 08:38

I\'m trying to change the background color of UITableViewHeaderFooterView. Although the view is appearing, the background color remains the default color. I\'m getting a log

20条回答
  •  春和景丽
    2020-12-23 09:29

    On 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];
    }
    

    On iOS8 I was using headerView.contentView.backgroundColor without problems, but now with iOS 9, I was getting a weird issue that made the background color not fill the whole space of the cell. So I tried just headerView.backgroundColor and I got the same error from the OP.

    Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead.

    So now everything works great and without warnings by using headerView.backgroundView.backgroundColor

提交回复
热议问题