如何在UITableView中更改节标题的颜色?
编辑 : DJ-S提供的答案应考虑适用于iOS 6及更高版本。 接受的答案已过时。
#1楼
不推荐在UITableViewHeaderFooterView上设置背景颜色。 请改用contentView.backgroundColor
。
#2楼
如果您想要具有自定义颜色的标题,可以执行此操作:
[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];
这个解决方案从iOS 6.0开始运行良好。
#3楼
如果您不想创建自定义视图,还可以更改颜色(需要iOS 6):
-(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
UIView* content = castView.contentView;
UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
content.backgroundColor = color;
}
}
#4楼
这是一个老问题,但我认为答案需要更新。
此方法不涉及定义和创建自己的自定义视图。 在iOS 6及更高版本中,您可以通过定义来轻松更改背景颜色和文本颜色
-(void)tableView:(UITableView *)tableView
willDisplayHeaderView:(UIView *)view
forSection:(NSInteger)section
部分委托方法
例如:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Background color
view.tintColor = [UIColor blackColor];
// Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];
// Another way to set the background color
// Note: does not preserve gradient effect of original header
// header.contentView.backgroundColor = [UIColor blackColor];
}
取自我的帖子: https : //happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/
斯威夫特3/4
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
view.tintColor = UIColor.red
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
}
#5楼
以下是更改文本颜色的方法。
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
label.text = @"Section Header Text Here";
label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];
来源:oschina
链接:https://my.oschina.net/stackoom/blog/3197584