Annabelle Sykes
Annabelle Sykes

Reputation: 131

CAGradientLayer frame not displaying properly

I have the following code to present a CAGradientLayer, which should cover the whole view. However, it shows up in the simulator not properly. I printed the values of self.view.bounds.width and self.view.bounds.height which printed the correct values. Below is also the simulator screenshot.

// 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

Upvotes: 1

Views: 284

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100541

You need to set frame not bounds

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

Upvotes: 1

Related Questions