Skip to content

Instantly share code, notes, and snippets.

@cwalo
Created January 10, 2018 19:44
Show Gist options
  • Save cwalo/3a75727f8fbd5e758fa13a2a0fcba440 to your computer and use it in GitHub Desktop.
Save cwalo/3a75727f8fbd5e758fa13a2a0fcba440 to your computer and use it in GitHub Desktop.
Override layoutSubviews to add a new layer using a path with a roundedRect and cornerRadius
private var shadowLayer: CAShapeLayer!
private var cornerRadius: CGFloat = 25.0
private var fillColor: UIColor = .blue // the color applied to the shadowLayer, rather than the view's backgroundColor
override func layoutSubviews() {
super.layoutSubviews()
if shadowLayer == nil {
shadowLayer = CAShapeLayer()
shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath
shadowLayer.fillColor = fillColor.cgColor
shadowLayer.shadowColor = UIColor.black.cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = CGSize(width: 0.0, height: 1.0)
shadowLayer.shadowOpacity = 0.2
shadowLayer.shadowRadius = 3
layer.insertSublayer(shadowLayer, at: 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment