Skip to content

Instantly share code, notes, and snippets.

@mathebox
Last active August 29, 2018 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathebox/4b1c8581446c489ecddb898855f3f065 to your computer and use it in GitHub Desktop.
Save mathebox/4b1c8581446c489ecddb898855f3f065 to your computer and use it in GitHub Desktop.
UIView with inner shadow in Swift
import UIKit
class InnerShadowView: UIView {
lazy var innerShadowLayer: CAShapeLayer = {
let shadowLayer = CAShapeLayer()
shadowLayer.shadowColor = UIColor.black.cgColor
shadowLayer.shadowOffset = CGSize(width: 0.0, height: 0.0)
shadowLayer.shadowOpacity = 0.1
shadowLayer.shadowRadius = 14
shadowLayer.fillRule = .evenOdd
return shadowLayer
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.configure()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.configure()
}
private func configure() {
self.layer.masksToBounds = true
self.layer.cornerRadius = 6
self.layer.addSublayer(self.innerShadowLayer)
}
override func layoutSubviews() {
super.layoutSubviews()
let shadowPath = CGMutablePath()
let inset = -self.innerShadowLayer.shadowRadius * 2.0
shadowPath.addRect(self.bounds.insetBy(dx: inset, dy: inset))
shadowPath.addRect(self.bounds)
self.innerShadowLayer.path = shadowPath
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment