Doj Yias Lem
Doj Yias Lem

Reputation: 137

How to add gradient colour to CALayer()

let bottomBorder = CALayer()
bottomBorder.backgroundColor = UIColor.viewShadowGray().cgColor
bottomBorder.frame = CGRect(x: 0, y: view.frame.size.height - 1, width: view.frame.size.width, height: 1)
view.layer.addSublayer(bottomBorder)

How to modify this to add gradient to it such that it looks like this: enter image description here

Upvotes: 1

Views: 540

Answers (1)

zisoft
zisoft

Reputation: 23078

Use a CAGradientLayer:

let gradientLayer = CAGradientLayer()
gradientLayer.frame = CGRect(x: 0, y: view.frame.size.height - 1, width: view.frame.size.width, height: 1)
gradientLayer.colors = [
    UIColor.white.withAlphaComponent(0.5).cgColor,
    UIColor.white.withAlphaComponent(0.0).cgColor
]
view.layer.addSublayer(gradientLayer)

Adjust the gradients colors to you needs.

Upvotes: 2

Related Questions