SwiftUI - 如何改变嵌套在表单中的选取器的颜色?

问题描述 投票:0回答:1

我试图改变SwiftUI中的复选标记的颜色,这个复选标记是在嵌套在Form中的Picker中使用的。我试着用以下方法来改变它

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)
                        }
                    }
                }
            }
        }
    }
}

enter image description here

swift swiftui uinavigationbar
1个回答
2
投票

这里是(用Xcode 11.4测试)

demo

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 !!
}

请注意,在Xcode 11.4中测试。 .accentColor 适用于所有 NavigationView 控制,所以 UINavigationBar.appearance().tintColor = .black 不需要。

© www.soinside.com 2019 - 2024. All rights reserved.