- (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];
}
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
// This changes the header background
view.tintColor = UIColor.blueColor()
// Gets the header view as a UITableViewHeaderFooterView and changes the text colour
var headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
headerView.textLabel.textColor = UIColor.redColor()
}
在iOS 7.0.4中,我使用自己的XIB创建了一个自定义标头 . 之前没有提到任何事情 . 它必须是UITableViewHeaderFooterView的子类才能使用 dequeueReusableHeaderFooterViewWithIdentifier: ,而且看起来这个类对于背景颜色非常顽固 . 所以最后我添加了一个名为customBackgroudView的UIView(你可以使用代码或IB),然后将它设置为's backgroundColor property. In layoutSubviews: I set that view'帧到边界 . It work with iOS 7 and gives no glitches.
// in MyTableHeaderView.xib drop an UIView at top of the first child of the owner
// first child becomes contentView
// in MyTableHeaderView.h
@property (nonatomic, weak) IBOutlet UIView * customBackgroundView;
// in MyTableHeaderView.m
-(void)layoutSubviews;
{
[super layoutSubviews];
self.customBackgroundView.frame = self.bounds;
}
// if you don't have XIB / use IB, put in the initializer:
-(id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
...
UIView * customBackgroundView = [[UIView alloc] init];
[self.contentView addSubview:customBackgroundView];
_customBackgroundView = customBackgroundView;
...
}
// in MyTableViewController.m
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
MyTableHeaderView * header = [self.tableView
dequeueReusableHeaderFooterViewWithIdentifier:@"MyTableHeaderView"];
header.customBackgroundView.backgroundColor = [UIColor redColor];
return header;
}
29 回答
基于@Dj S答案,使用Swift 3.这在iOS 10上运行良好 .
在iOS 7.x中,我有一个使用静态表视图单元的项目 . willDisplayHeaderView不会触发 . 但是,这种方法可行:
以下是更改文本颜色的方法 .
希望
UITableViewDelegate
协议中的这个方法可以帮助您入门:Objective-C:
Swift:
2017年更新:
Swift 3:
将
[UIColor redColor]
替换为您想要的UIColor
. 您可能还希望调整headerView
的尺寸 .这是一个老问题,但我认为答案需要更新 .
此方法不涉及定义和创建自己的自定义视图 . 在iOS 6及更高版本中,您可以通过定义来轻松更改背景颜色和文本颜色
部分委托方法
例如:
取自我的帖子:https://happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/
Swift 3
如果您想要具有自定义颜色的 Headers ,可以执行此操作:
这个解决方案从iOS 6.0开始运行良好 .
以下解决方案适用于 Swift 1.2 with iOS 8+
不要忘记从代理中添加这段代码,或者在某些情况下,相对于视图/标签的高度,您的视图将被截断或显示在表格后面 .
如果您不想创建自定义视图,还可以更改颜色(需要iOS 6):
不推荐在UITableViewHeaderFooterView上设置背景颜色 . 请改用
contentView.backgroundColor
.设置截面区域的背景和文本颜色:(感谢
William Jockusch
和Dj S
)你可以在大约2秒内在main.storyboard上完成 .
选择表格视图
转到属性检查器
列出项目
向下滚动到查看子 Headers
更改"background"
以下是在 Headers 视图中添加图像的方法:
Swift 4
要更改UITableView部分的 Headers 视图的 background color , text label color 和 font ,只需为表视图覆盖
willDisplayHeaderView
,如下所示:这对我很有用;希望它对你也有帮助!
对于iOS8(Beta)和Swift,选择你想要的RGB颜色,试试这个:
}
(“覆盖”是因为我在我的项目中使用UITableViewController而不是普通的UIViewController,但它不是强制更改节 Headers 颜色)
您的 Headers 文字仍会显示 . 请注意,您需要调整节 Headers 高度 .
祝好运 .
SWIFT 2
我能够通过添加模糊效果(这非常酷)成功更改了部分背景颜色 . 要轻松更改部分的背景颜色:
首先转到Storyboard并选择Table View
转到属性检查器
列出项目
向下滚动到“查看”
更改"Background"
然后对于模糊效果,添加到代码:
我知道它的回答,以防万一,在Swift中使用以下内容
iOS 8+
我认为这段代码并不是那么糟糕 .
在iOS 7.0.4中,我使用自己的XIB创建了一个自定义标头 . 之前没有提到任何事情 . 它必须是UITableViewHeaderFooterView的子类才能使用
dequeueReusableHeaderFooterViewWithIdentifier:
,而且看起来这个类对于背景颜色非常顽固 . 所以最后我添加了一个名为customBackgroudView的UIView(你可以使用代码或IB),然后将它设置为's backgroundColor property. In layoutSubviews: I set that view'帧到边界 . It work with iOS 7 and gives no glitches.只需更改 Headers 视图图层的颜色即可
如果有人需要迅速,保持 Headers :
在我的情况下,它的工作方式如下:
使用RubyMotion / RedPotion,将其粘贴到TableScreen中:
奇迹般有效!
我通过控制台日志从Xcode收到消息
然后我创建一个新的UIView并将其作为HeaderView的背景 . Xcode表示,这不是一个好的解决方案,但很容易 .
虽然
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
也可以正常工作,但您可以在不实现其他委托方法的情况下实现此目的 . 在你func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
方法中,你可以使用view.contentView.backgroundColor = UIColor.white
而不是view.backgroundView?.backgroundColor = UIColor.white
这是无效的 . (我知道backgroundView
是可选的,但即使它在那里,如果没有实现willDisplayHeaderView
也不会这样做使用UIAppearance,您可以为应用程序中的所有 Headers 更改它,如下所示:
UITableViewHeaderFooterView.appearance() . backgroundColor =theme.subViewBackgroundColor
只需设置背景视图的背景颜色: