CAGradientLayer 框架显示不正确
CAGradientLayer frame not displaying properly
我有以下代码来呈现一个 CAGradientLayer
,它应该涵盖整个视图。但是,它没有正确显示在模拟器中。我打印了 self.view.bounds.width
和 self.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)
您需要设置 frame
而不是 bounds
gradientLayer.frame = self.view.frame // here frame = bounds
我有以下代码来呈现一个 CAGradientLayer
,它应该涵盖整个视图。但是,它没有正确显示在模拟器中。我打印了 self.view.bounds.width
和 self.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)
您需要设置 frame
而不是 bounds
gradientLayer.frame = self.view.frame // here frame = bounds