iOS 9 TableView分组Header/Footer设置背景色

项目开发在测试阶段,突然发现一个问题,就是在iOS 9系统中UITableView的设置分组HeaderView或者FooterView背景色可能无用。做个笔记,记录一下,希望可以帮到一些同仁。

我在项目中是这样写的:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    NSDictionary *dict = self.viewModel.dataSource[section][0];
    YDTitleValueTableHFView *head = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[YDTitleValueTableHFView kk_reuseIdentifier]];
    head.contentView.backgroundColor = [UIColor yd_colorF3F3F3];
    head.titleLabel.font = [UIFont systemFontOfSize:12];
    head.titleLabel.textColor = [UIColor whiteColor];
    head.titleLeftMargin = 16;
    head.titleLabel.text = dict[@"pre"];
    return head;
}

但是我发现,设置head.contentView.backgroundColor在iOS9上貌似不起作用,经过测试,发现以下方式可以解决:

  1. 直接在代理方法中返回一个设置好背景色的View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
    headerView.backgroundColor = [UIColor whiteColor];
    return headerView;
}
  1. 设置UITableViewHeaderFooterViewbackgroundView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[UITableViewHeaderFooterView kk_reuseIdentifier]];
    view.backgroundView = ({
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        view;
    });
    return view;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容