代码之家  ›  专栏  ›  技术社区  ›  Annabelle Sykes

caGradientLayer帧显示不正确

  •  0
  • Annabelle Sykes  · 技术社区  · 7 年前

    我有下面的代码来显示一个 cagradientlayer ,它应该覆盖整个视图。但是,它在模拟器中显示不正确。我打印了 self.view.bounds.width self.view.bounds.height which打印了正确的值。下面是模拟器屏幕截图。

    向日历视图添加背景层 让渐变层=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(渐变层,位于:0) < /代码>

    . 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)
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   Shehata Gamal    7 年前

    你需要设置 frame bounds

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