码迷,mamicode.com
首页 > 其他好文 > 详细

改变UITableView的headerView、footerView背景颜色

时间:2016-07-20 15:04:34      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:

问题

改变UITableView的header、footer背景颜色,这是个很常见的问题。之前知道的一般做法是,通过实现tableView: viewForHeaderInSection:返回一个自定义的View,里面什么都不填,只设背景颜色。但是今天发现一个更简洁的做法。

更简洁的方法

对于iOS 6及以后的系统,实现这个新的delegate函数即可:

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
    view.tintColor = [UIColor clearColor];
}

还可以改变文字的颜色:

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view;
    [footer.textLabel setTextColor:[UIColor whiteColor]];
}

错误的尝试

写这篇文章的目的,主要是想记录两种错误的尝试。
当看到这个Delegate函数时,第一反应是想当然地这样做:

错误尝试1

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
    view.backgroundColor = [UIColor clearColor];
}

这样做是无效的,无论对什么颜色都无效。

错误尝试2

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view;
    footer.contentView.backgroundColor = [UIColor redColor];
}

这样做设成不透明的颜色就没问题。但设成clearColor,看到的还是灰色。



文/戴仓薯(简书作者)
原文链接:http://www.jianshu.com/p/bfb237f5c20c
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

改变UITableView的headerView、footerView背景颜色

标签:

原文地址:http://www.cnblogs.com/handada/p/5688279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!