Skip to content

Instantly share code, notes, and snippets.

@fuxingloh
Created June 23, 2017 15:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fuxingloh/a69ecef7db2db9577f4d605766ba7087 to your computer and use it in GitHub Desktop.
Save fuxingloh/a69ecef7db2db9577f4d605766ba7087 to your computer and use it in GitHub Desktop.
How to add gradient on top and bottom of image view.
// Call this in layoutSubView or viewDidLoad
self.imageView.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
let width = self.bounds.width
let height = self.bounds.height
let sHeight:CGFloat = 60.0
let shadow = UIColor.black.withAlphaComponent(0.6).cgColor
// Add gradient bar for image on top
let topImageGradient = CAGradientLayer()
topImageGradient.frame = CGRect(x: 0, y: 0, width: width, height: sHeight)
topImageGradient.colors = [shadow, UIColor.clear.cgColor]
imageView.layer.insertSublayer(topImageGradient, at: 0)
let bottomImageGradient = CAGradientLayer()
bottomImageGradient.frame = CGRect(x: 0, y: height - sHeight, width: width, height: sHeight)
bottomImageGradient.colors = [UIColor.clear.cgColor, shadow]
imageView.layer.insertSublayer(bottomImageGradient, at: 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment