【问题标题】:How to chose Color depending on System Background Color in Swift 5?如何在 Swift 5 中根据系统背景颜色选择颜色?
【发布时间】:2021-03-29 12:04:52
【问题描述】:

我目前正在使用SwiftUI Colors

问题:我有一个Card Component,其背景颜色为systemGray6。这适用于大多数视图(所有具有systemBackground 背景颜色的视图)

但是,我有一些 Modal Views 附带 tertiarySystemBackground。这不会更改LightMode 中的任何内容,但会将DarkMode 中的背景颜色更改为与systemGray6 相同。结果,背景和卡片的颜色相同,卡片不再可见。

到目前为止我的解决方案:

struct Card: View {
    var isInsideModal: Bool = false

    var body: some View {
        // Some Content
        .background(isInsideModal ? Color.systemGray6 : Color.systemGray5) // I added this in an Extension of Color
    }
}

问题:是否有可能自动执行此行为,而无需始终将所需的Background Color 明确传递给Card View

感谢您的帮助。

【问题讨论】:

  • 你说的自动化是什么意思?
  • @Asperi 我的意思是 CardView 会根据父视图的背景颜色自行确定选择哪种背景颜色。这样您就不必提供此信息作为参数。也许父母视图 bgColor 作为环境变量或类似的东西传递?

标签: swift view colors swiftui background


【解决方案1】:

您可以在 xcassets 文件夹中创建颜色集。它使您能够根据您的应用是在暗模式还是亮模式下运行来选择不同的外观。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-04
    • 2011-03-17
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多