Skip to content

Instantly share code, notes, and snippets.

@iOSDigital
Created February 25, 2019 13:04
Show Gist options
  • Save iOSDigital/7f08d77da8d5684120d35fc8296716e9 to your computer and use it in GitHub Desktop.
Save iOSDigital/7f08d77da8d5684120d35fc8296716e9 to your computer and use it in GitHub Desktop.
Swift UIView extension to add rounded corners and a drop shadow to a view, avoiding the clipsToBounds problem
import Foundation
import UIKit
public extension UIView {
func roundCornerWithShadow(cornerRadius: CGFloat, shadowRadius: CGFloat, offsetX: CGFloat, offsetY: CGFloat, colour: UIColor, opacity: Float) {
self.clipsToBounds = false
let layer = self.layer
layer.masksToBounds = false
layer.cornerRadius = cornerRadius
layer.shadowOffset = CGSize(width: offsetX, height: offsetY);
layer.shadowColor = colour.cgColor
layer.shadowRadius = shadowRadius
layer.shadowOpacity = opacity
layer.shadowPath = UIBezierPath(roundedRect: layer.bounds, cornerRadius: layer.cornerRadius).cgPath
let bColour = self.backgroundColor
self.backgroundColor = nil
layer.backgroundColor = bColour?.cgColor
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment