编辑:iOS7+ 的当前最佳解决方案
在你的 UITableViewDelegate 中实现一个 tableView 方法:
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView,
forSection section: Int) {
// Background color
view.tintColor = UIColor.blackColor()
// Text Color/Font
let header = view as UITableViewHeaderFooterView
header.textLabel.textColor = UIColor.greenColor()
header.textLabel.font = my_font // put the font you want here...
}
下面的原始帖子(过时的hacky解决方案)
调查
搜索有关该主题的少量 Apple 文档和 SO Q&A,我无法弄清楚如何使用 UITraitCollection。
不过,我似乎在 this post in SO 中发现了一个可能的 hack。
我已经用下面的实现对其进行了测试,它似乎可以工作。
我们需要采取三个步骤来创建一个 Objective-C 类方法来完成这项工作。使用桥接头,我们可以无缝地从 Swift 调用方法。
添加到桥接头
#import "BridgeAppearance.h"
创建 BridgeAppearance.h
@interface BridgeAppearance : NSObject { }
+ (void)setFontForUITableHeaderFooters: (UIFont*)font;
@end
创建 BridgeAppearance.m
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "BridgeAppearance.h"
@implementation BridgeAppearance
+ (void)setFontForUITableHeaderFooters: (UIFont*)font
{
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil]
setFont: font];
}
@end