CAGradientLayer框架无法正确显示

时间:2018-08-06 13:52:33

标签: swift cagradientlayer

我有以下代码来呈现CAGradientLayer,它应该覆盖整个视图。但是,它不能正确显示在模拟器中。我打印了self.view.bounds.widthself.view.bounds.height的值,这些值打印了正确的值。下面也是模拟器的屏幕截图。

// Add background layer to calendar view
    let gradientLayer = CAGradientLayer()
    gradientLayer.bounds = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
    gradientLayer.colors = [lightDarkColor.cgColor, darkColor.cgColor, lightDarkColor.cgColor]
    gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
    self.view.layer.insertSublayer(gradientLayer, at: 0)

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要设置frame而不是bounds

gradientLayer.frame = self.view.frame // here frame = bounds