How to do Gradient background colors in Swift ?

Subscribe to my newsletter and never miss my upcoming articles

Hello folks Leo here,

Quick tip from what I learned today.

The code below make the background from white to yellowish. Obs: self here is the element that you want to have a gradient background

        let colorTop = UIColor.white.cgColor
        let colorBottom = UIColor(red: 0.941 , green: 0.818, blue: .zero, alpha: 1.0).cgColor

        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = self.bounds
        gradientLayer.colors = [colorTop, colorBottom]
        self.layer.insertSublayer(gradientLayer, at: 0)

Thanks for reading!

MilanIask's photo

Remember how my teacher asked me to print this codes for him. I've used almost whole cartridge for that. And this cartridges are so expensive.

Lucya Smit's photo

It looks interesting. I was very curious, how much toner will it take to print such a test image in CMYK? I'm thinking of trying it on my Brother printer. I still buy toner cheap on the site https://www.mrdepot.ca/products/brother-mfc-7860dw-toner , so I'm not afraid to spend a little bit on printing such a gradient. Both beautiful and practical.