【问题标题】:Is it possible to override dark mode for a form only?是否可以仅覆盖表单的暗模式?
【发布时间】:2021-03-19 02:54:03
【问题描述】:

我想要实现的是具有不同背景颜色的表单。但我希望里面的项目始终使用浅色模式变体(深色文本颜色)。但不是完整的视图。我已经搜索过,但找不到任何解决方案。

我尝试listRowBackground 更改有效项目的背景,preferredColorScheme 使用灯光模式,但这将使完整视图处于灯光模式:

...

Form {

    DatePicker(selection: $date, displayedComponents: .date) { ... }
        .listRowBackground(Color.yellow)

    TextField("Name", text: $name)
        .listRowBackground(Color.yellow)

    Picker("amount", selection: $amount) {
        ForEach(amounts, id: \.self) {
            Text($0)
        }
    }
    .listRowBackground(Color.yellow)

    Button(action: { ... }) {
        Label("More", systemImage: "chevron.up")
    }
    .listRowBackground(Color.yellow)

}
.preferredColorScheme(.light)

...

【问题讨论】:

    标签: ios swiftui


    【解决方案1】:

    您可以使用.environment 来强制视图的配色方案:

    .environment(\.colorScheme, .light)
    

    如果您不想对整个表单执行此操作,则可以仅将其注入嵌套视图:

    Form {
        Group {
            DatePicker(selection: $date, displayedComponents: .date) { ... }
                .listRowBackground(Color.yellow)
    
            TextField("Name", text: $name)
                .listRowBackground(Color.yellow)
    
            Picker("amount", selection: $amount) {
                ForEach(amounts, id: \.self) {
                    Text($0)
                }
            }
            .listRowBackground(Color.yellow)
    
            Button(action: { ... }) {
                Label("More", systemImage: "chevron.up")
            }
            .listRowBackground(Color.yellow)
        }
        .environment(\.colorScheme, .light)
    }
    

    (或仅针对单个视图)

    Form {
        // ...
    
        TextField("Name", text: $name)
            .listRowBackground(Color.yellow)
            .environment(\.colorScheme, .light)
    
        // ...
    }
    

    【讨论】:

    • 我已经尝试过了,并且还将完整视图更新为灯光模式。
    • @Bobbify 我不确定我是否完全理解您的回答,但请查看更新。
    【解决方案2】:

    您可以使用foregroundColor(Color.black)accentColor(Color.black) 进行这项工作:


    import SwiftUI
    
    struct ContentView: View {
        
        @State private var string: String = String()
        
        @State var selectedColor: Int = 2
        let colorArray: [String] = ["Black", "Blue", "Gray", "Green"]
    
        
        var body: some View {
            
            VStack(spacing: 20) {
                
                Text("This text has a dark text color!")
                    .foregroundColor(Color.black)
                    .padding()
        
                TextField("Enter your text here. . .", text: $string)
                    .background(Color.red)
                    .foregroundColor(Color.black)
    
                Picker(selection: $selectedColor, label: Text(String())) {
    
                    ForEach(colorArray.indices, id: \.self) { index in
    
                        Text(colorArray[index])
                            .foregroundColor(Color.black)
                        
                    }
    
                }
    
                
                Text("This text has a normal text color!")
                    .foregroundColor(Color.primary)
                    .padding()
                
            }
            .padding()
            .background(Color.red)
            .cornerRadius(10)
      
        }
    }
    

    【讨论】:

    • 这确实适用于普通文本元素,但不适用于 datpicker、文本字段和选择器等表单元素。这些有诸如占位符、标签和活动状态之类的东西。
    • 我对文本字段的占位符文本和未改变颜色的选择器的 V 形有问题。
    • 感谢您的调查。我已经设法通过@pawello2222 的解决方案让它工作(更少的代码)。
    • @Bobbify:很高兴听到它,两种方式都很好,代码更少,解决你的问题的都用它没问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多