【发布时间】:2021-01-28 16:57:24
【问题描述】:
在我的 SwiftUI View 我有一个 Image 我从资产加载。我已将foregroundColor 设置为accentColor,我还在资产中将其设置为我的自定义AccentColor,它将在整个应用程序中使用。
资产:
内容视图正文:
var body: some View {
Image("MyImageName")
.resizable()
.renderingMode(.template)
.foregroundColor(.accentColor)
.frame(width: 32, height: 32, alignment: .center)
}
预览和模拟器在右侧显示图像foregroundColor。但是,在物理设备上运行时,foregroundColor 不知何故仍然是手机的默认蓝色accentColor。
预览:
模拟器:
真机:
为什么会这样?以及如何确保 AssentColor 跨设备工作,无论 iOS 版本如何,而无需修改 iPhone 上的任何设置?调试详情:设备iOS版本为iOS 13.5.1和Xcode 12.0.1。
【问题讨论】:
-
.accentColor 已在系统中定义。您究竟是如何以及在何处尝试重新定义它的?最后,为什么不直接将 foregroundColor 设置为资产颜色?
-
检查我在 OP 中指定的 Xcode 和 iOS 的版本。您会发现
.accentColor在真实设备上的工作方式不如在模拟器上的预期效果。
标签: swift image colors swiftui assets