Skip to content

Instantly share code, notes, and snippets.

@foxicode
Last active April 16, 2020 08:10
Show Gist options
  • Select an option

  • Save foxicode/952d7459a95a3fc0d54e1e0fb8c6e24f to your computer and use it in GitHub Desktop.

Select an option

Save foxicode/952d7459a95a3fc0d54e1e0fb8c6e24f to your computer and use it in GitHub Desktop.
Swift UIKit shadows
import UIKit
extension UIView {
@IBInspectable var dropShadow: Bool {
set {
layer.shadowOffset = CGSize(width: 0, height: 0)
if newValue {
updateShadow()
} else {
layer.shadowColor = UIColor.clear.cgColor
layer.shadowOpacity = 0.0
layer.shadowRadius = 0
layer.shadowPath = nil
layer.shouldRasterize = false
}
}
get {
layer.shadowOpacity > 0.0 && layer.shadowRadius > 0
}
}
func updateShadow() {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.5
layer.shadowRadius = 4
layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: layer.cornerRadius).cgPath
layer.shouldRasterize = true
layer.rasterizationScale = UIScreen.main.scale
}
}
import UIKit
extension UIView {
func updateShadows() {
if self.dropShadow {
self.updateShadow()
}
subviews.forEach {
$0.updateShadows()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment