let cellview: UIView!
let gradient = CAGradientLayer()
let topColor = UIColor(red: 0.435, green: 0, blue: 0.635, alpha: 1.0)
let bottomColor = UIColor(red: 0.255 , green: 0.043, blue: 0.373, alpha: 1.0)
gradient.colors = [topColor.cgColor, bottomColor.cgColor]
gradient.startPoint = CGPoint(x:0, y:0)
gradient.endPoint = CGPoint(x:0.3, y:1)
cellview = UIView(frame: CGRect(x: 0, y: 0, width: 192, height: 160))
gradient.frame = cellview.bounds
gradient.masksToBounds = true
cellview.layer.addSublayer(gradient)
I wrote the above code according to the code gradient should cover entire view. But this is how the gradient appearing.
Here's a function i use to create gradients, you may find it useful
func createGradientBackground(gradientColor: UIColor, darkStylePreferred: Bool, viewToUse: UIView) {
let gradient = CAGradientLayer(layer: viewToUse.layer)
gradient.frame = viewToUse.bounds
gradient.locations = [0, 1]
viewToUse.layer.insertSublayer(gradient, at: 0)
let animation = CABasicAnimation(keyPath: "locations")
animation.toValue = [0, 0.11, 1]
animation.fromValue = [0, 0.9, 1]
if darkStylePreferred {
gradient.colors = [UIColor.black.cgColor, gradientColor.cgColor, UIColor.black.cgColor]
} else {
gradient.colors = [UIColor.white.cgColor, gradientColor.cgColor, UIColor.white.cgColor]
}
animation.autoreverses = true
animation.repeatCount = Float.infinity
animation.speed = 0.015
animation.isRemovedOnCompletion = false
gradient.add(animation, forKey: nil)
}