[英]SwiftUI Gradient rendering incorrect colors on simulator
When running on the simulator, Gradient
is rendering colors incorrectly.在模拟器上运行时,
Gradient
渲染颜色不正确。 When running on a device, Gradient
renders the colors correctly.在设备上运行时,
Gradient
会正确呈现颜色。 How can I get Gradient
to render colors correctly on the simulator so that I can capture accurate screenshots?如何让
Gradient
在模拟器上正确渲染颜色,以便我可以捕获准确的屏幕截图?
Simulator versus device:模拟器与设备:
Example View
with Gradient
:带有
Gradient
示例View
:
struct GradientView: View {
private static let backgroundGradientColors: [Color] = [.red, .blue]
var body: some View {
ZStack {
GeometryReader { geometryReader in
let gradient: Gradient = Gradient(colors: GradientView.backgroundGradientColors)
RadialGradient(gradient: gradient,
center: .bottomTrailing,
startRadius: 0, endRadius: geometryReader.size.width)
.edgesIgnoringSafeArea(.all)
}
}
}
}
This seems to happen if you use the built-in system colours.如果您使用内置系统颜色,这似乎会发生。 Once I changed to using asset catalog colours that have light and dark variants, they appeared correctly.
一旦我改为使用具有浅色和深色变体的资产目录颜色,它们就会正确显示。
PS I had to delete the app from the sim each time I changed the colours to make them show correctly in the widget preview. PS 每次更改颜色以使其在小部件预览中正确显示时,我都必须从 sim 中删除该应用程序。
I just ran into this too.我也刚碰到这个。 It obviously seems to be a bug in Xcode/Simulator, as everything looks correct on device.
这显然似乎是 Xcode/Simulator 中的一个错误,因为设备上的一切看起来都是正确的。
This happens on my MacBook Pro 15" when it's attached to an external monitor, but interestingly everything appears totally normal in the SwiftUI preview and on Simulator when I run it on the normal built-in display.当我的 MacBook Pro 15" 连接到外部显示器时会发生这种情况,但有趣的是,当我在普通的内置显示器上运行它时,在 SwiftUI 预览和模拟器上一切都显得完全正常。
So if you run into this and you're on a laptop with an external monitor, try unplugging and see if that helps.因此,如果您遇到这种情况并且您使用的是带有外接显示器的笔记本电脑,请尝试拔掉电源,看看是否有帮助。
If your gradient is drawn on top of a white background for the purposes of previewing in the simulator and Xcode canvas area, you can work around the bug by using a different blendMode
(as mentioned here ):如果您的梯度在白色背景上在模拟器和Xcode的画布区域预览的目的顶部绘制,则可以解决该漏洞通过使用不同
blendMode
(如提到这里):
/// Work around Xcode bug where SwiftUI gradients show up as green in the Simulator
let workAroundBlendMode: BlendMode = {
#if targetEnvironment(simulator)
return BlendMode.plusDarker
#else
return BlendMode.normal
#endif
}()
RadialGradient(...)
.blendMode(workAroundBlendMode)
The #if targetEnvironment(simulator)
protects your release builds from the issues that come up when the background is not white, for example in Dark Mode. #if targetEnvironment(simulator)
保护您的发布版本免受背景不是白色时出现的问题的影响,例如在黑暗模式下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.