为什么线性渐变在模拟器中运行良好但在真实设备上运行不佳

Why is linear gradient working well in simulator but not on real device

我正在使用以下代码为 iOs 按钮生成线性渐变

private func applyGradient(colors: [CGColor])
{
    gradientLayer.colors = colors
    gradientLayer.startPoint = CGPoint(x: 0, y: 1)
    gradientLayer.endPoint = CGPoint(x: 0, y: 0)
    self.addToCart.layer.insertSublayer(gradientLayer, at: 0)
}

 override func viewDidLayoutSubviews(){
    gradientLayer.frame = self.addToCart.bounds
}
override func viewDidLoad() {
    super.viewDidLoad()
    self.applyGradient(colors: [UIColor(red: 234, green: 67, blue: 53, alpha: 0.06).cgColor, 
    UIColor(red: 234, green: 67, blue: 53, alpha: 1).cgColor])   
  
}

渐变在模拟器上运行良好,但在真实设备上运行不正常 这是我在模拟器上得到的

这是我在真实设备上得到的

完全不同

RGB 颜色通道值无效。 UIColor(red:green:blue:alpha:)the documentation。 RGBA 值应介于 0 和 1 之间。如果您查看控制台,您可能会看到类似以下内容的错误:

[Graphics] UIColor created with component values far outside the expected range. Set a breakpoint on UIColorBreakForOutOfRangeColorComponents to debug. This message will only be logged once.

您应该将 RGB 值除以 255:

applyGradient(colors: [
    UIColor(red: 234/255, green: 67/255, blue: 53/255, alpha: 0.06).cgColor,
    UIColor(red: 234/255, green: 67/255, blue: 53/255, alpha: 1).cgColor
])

仅供参考,如果您想在 UIColorBreakForOutOfRangeColorComponents 上添加断点,请参阅 Setting Breakpoints to Pause Your Running App 中的“在代码外的符号上暂停”,例如添加符号断点:

并指定UIColorBreakForOutOfRangeColorComponents符号:

并且调试器将在有问题的行处停止: