swift - SwiftUI - 如何更改嵌套在表单中的选取器的复选标记颜色
问题描述
我正在尝试更改 SwiftUI 中复选标记的颜色,该复选标记用于嵌套在表单内的选取器中。我试过了:
UINavigationBar.appearance().tintColor = .black
但这只会改变“< Back”的颜色。
struct ContentView: View {
@State private var selectedMode = 0
private var modes = ["#1", "#2"]
var body: some View {
NavigationView {
Form {
Section(header:Text("").font(.title)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
){
Picker(selection: $selectedMode, label: Text("Modes")) {
ForEach(0 ..< modes.count, id: \.self) {
Text(self.modes[$0])
.foregroundColor(Color.red)
}
}
}
}
}
}
}
解决方案
在这里(使用 Xcode 11.4 测试)
var body: some View {
NavigationView {
Form {
Section(header:Text("").font(.title)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
){
Picker(selection: $selectedMode, label: Text("Modes")) {
ForEach(0 ..< modes.count, id: \.self) {
Text(self.modes[$0])
.foregroundColor(Color.red)
}
}
}
}
}.accentColor(Color.black) // << fix !!
}
注意: .accentColor
适用于所有NavigationView
控件,因此UINavigationBar.appearance().tintColor = .black
不需要。
推荐阅读
- python - 用numpy上的数组索引数组
- flutter - 无论如何在flutter web的新标签中打开一个页面?
- javascript - 在循环中从异步函数获取数据
- amazon-web-services - Amazon S3 复制在使用预签名 URL 上传后失败,但在通过 AWS 接口上传后成功
- postgresql - 如何使用多个参数调用函数 - postgresql 11
- python-3.x - 该代码工作不正确并显示难以理解的数字:
- laravel-7 - Laravel 获取 hasMany 关系的计数
- python - 使用 testbook 测试 Jupyter notebook 单元时如何修补 input()?
- mysql - 将多个电话号码加入列:phone1、phone2、phone3
- python - 只是一个关于在 python 和 ACSII 中添加引号的快速问题