iOS15升级后UITableView Section头部Label字号颜色改变,求全局解决方案
原因说明
iOS15对UITableView的默认样式做了两处改动导致出现差异:
- 调整了UITableViewHeaderFooterView的
textLabel默认配置,Plain样式的section header默认字号从17号改为15号,文本颜色调整为系统二级灰 - 新增了
sectionHeaderTopPadding属性,默认值为22,会额外增加section头部的顶部间距
全局解决方案
可以通过UIAppearance协议在App启动时全局配置,不需要逐个修改每个UITableView的代理实现,代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if (@available(iOS 15.0, *)) { // 全局设置section头部/尾部的文本样式 [[UILabel appearanceWhenContainedInInstancesOfClasses:@[[UITableViewHeaderFooterView class]]] setTextColor:[UIColor colorWithRed:0.137f green:0.137f blue:0.137f alpha:1.0]]; [[UILabel appearanceWhenContainedInInstancesOfClasses:@[[UITableViewHeaderFooterView class]]] setFont:[UIFont boldSystemFontOfSize:17]]; // 可选:全局关闭默认新增的section头部顶部间距,不需要额外间距可加上这行 [[UITableView appearance] setSectionHeaderTopPadding:0]; } // 其他原有启动逻辑 return YES; }
注意事项
如果项目中有少数特殊的UITableView不需要使用全局样式,可以在对应ViewController中单独为该UITableView设置样式,局部配置的优先级高于全局UIAppearance配置,不会被全局样式覆盖。
内容的提问来源于stack exchange,提问作者hehe5255887163com
相关产品推荐
相关产品推荐

