【发布时间】: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