简体   繁体   English

SwiftUI - 如何更改嵌套在表单中的选取器的复选标记颜色

[英]SwiftUI - How to change the checkmark color of a Picker which is nested in a Form

I'm trying to change the color of the checkmark in SwiftUI which is used in a Picker nested inside a Form.我正在尝试更改 SwiftUI 中复选标记的颜色,该复选标记用于嵌套在表单中的选取器中。 I tried it with:我试过了:

UINavigationBar.appearance().tintColor =.black UINavigationBar.appearance().tintColor =.black

but that only changed the "< Back" color.但这只会改变“< 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)
                        }
                    }
                }
            }
        }
    }
}

在此处输入图像描述

Here it is (tested with Xcode 11.4)在这里(用 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 !!
}

Note: .accentColor is applied for all NavigationView controls, so UINavigationBar.appearance().tintColor =.black is not needed.注意: .accentColor适用于所有NavigationView控件,因此不需要UINavigationBar.appearance().tintColor =.black

backup 备份

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM