Skip to content

Instantly share code, notes, and snippets.

@monsoir
Created April 3, 2021 08:16
Show Gist options
  • Save monsoir/8b5f8016b989e17b06d18f23c7c648ff to your computer and use it in GitHub Desktop.
Save monsoir/8b5f8016b989e17b06d18f23c7c648ff to your computer and use it in GitHub Desktop.
// use in a custom view
override func layoutSubviews() {
super.layoutSubviews()
setupShadow()
}
private var shadowLayer: CAShapeLayer! // layer for shadow back
private func setupShadow() {
guard columnStackView.bounds.size != .zero else { return }
if shadowLayer == nil {
shadowLayer = CAShapeLayer()
shadowLayer.path = UIBezierPath(roundedRect: <target view>.frame, cornerRadius: <shadow corner radius>).cgPath
shadowLayer.fillColor = <shadow color>.cgColor
shadowLayer.shadowColor = <shadow color>.cgColor
shadowLayer.shadowPath = shadowLayer.path
// width for x offset according to <target view>'s frame
// height for y offset according to <target view>'s frame
shadowLayer.shadowOffset = CGSize(width: 0, height: 15)
shadowLayer.shadowOpacity = 1
shadowLayer.shadowRadius = 8
}
layer.insertSublayer(shadowLayer, at: 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment