Xcode13下如何修改compact样式UIDatePicker的默认黑色文字颜色
解决.compact样式UIDatePicker文字颜色修改问题
适配iOS14/15系统,以下是经过验证的可行方案:
- 方案1:全版本兼容(iOS14+通用)
通过setTitleTextAttributes方法直接设置指定状态的文字颜色,无需依赖私有接口:
// 修改正常状态下的文字颜色 pickerInizio.setTitleTextAttributes([.foregroundColor: UIColor.red], for: .normal) // 可选:修改高亮/选中状态的文字颜色 pickerInizio.setTitleTextAttributes([.foregroundColor: UIColor.lightGray], for: .highlighted)
如果需要全局统一修改所有日期选择器的样式,可使用UIAppearance批量配置:
UIDatePicker.appearance().setTitleTextAttributes([.foregroundColor: UIColor.red], for: .normal)
- 方案2:iOS15+官方专属配置
iOS15之后苹果提供了官方的属性配置入口,稳定性更高,无审核风险:
if #available(iOS 15.0, *) { pickerInizio.titleTextAttributes = AttributeContainer([ .foregroundColor: UIColor.red ]) }
注意事项:
- 原有
setValue(UIColor.red, forKey: "textColor")属于私有API调用,不仅在.compact样式下失效,还存在苹果审核被拒的风险,不建议使用tintColor仅能修改弹出日历页的选中标识颜色,无法修改控件展示的日期文字颜色

内容的提问来源于stack exchange,提问作者Francesco
相关产品推荐
相关产品推荐

