ios - 是否可以仅覆盖表单的暗模式?

标签 ios swiftui

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

我尝试使用 listRowBackground 更改有效项目的背景,并尝试 preferredColorScheme 使用浅色模式,但这将使完整 View 处于浅色模式:

...

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)

...

最佳答案

您可以使用.environment强制 View 的配色方案:

.environment(\.colorScheme, .light)

如果您不想对整个表单执行此操作,则可以仅将其注入(inject)到嵌套 View 中:

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

(或仅单个 View )

Form {
    // ...

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

    // ...
}

关于ios - 是否可以仅覆盖表单的暗模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66695827/

相关文章:

ios - SwiftUI:动画期间的文本问题

ios - Swift:从 sprite 工具包 SKShapeNode 图纸中检测交点

ios - 警告 : attempt to present whose view is not in the window hierarchy

ios - UITableView 中单元格顶部的 UIView

swift - 如何在 VStack Swift UI 中居中按钮

ios - SwiftUI View 不会在单独的 View 中更新

swift - 在 SwiftUI macOS 应用程序中禁用或删除打印菜单项

ios - UIWindow 上的偏移导致标签栏在向后滑动时显示两次

ios - 查找 Image(systemName :) in SwiftUI) 的所有可用图像

macos - 删除/更改列表突出显示颜色(或调整项目之间的空间)