Alfred
Alfred

Reputation: 25

Gradient Layer background

I am trying to create a gradient background. i have followed steps on different tutorials but cannot get my background view to appear, I just get a white background. I cannot understand what I am doing wrong or not doing. Code is below. Help greatly appreciated.

import UIKit

extension CAGradientLayer {

    func backgroundGradient() -> CAGradientLayer {

        self.colors = [UIColor.green,UIColor.blue]
        self.locations = [0.0, 1.0]
        self.startPoint = CGPoint(x: 0.0, y: 1.0)
        self.endPoint = CGPoint(x: 1.0, y: 1.0)

        return self
    }
}


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let gradient = CAGradientLayer().backgroundGradient()

        gradient.frame = self.view.bounds
        print(gradient.frame)
        print(gradient.startPoint)
        self.view.layer.insertSublayer(gradient, at: 0)

    }
}

Upvotes: 0

Views: 64

Answers (1)

DonMag
DonMag

Reputation: 77482

Gradient layers take CGColor instead of UIColor. Easy fix...

In your extension, change the self.colors line to:

self.colors = [UIColor.green.cgColor, UIColor.blue.cgColor]

Upvotes: 1

Related Questions