Created
February 25, 2019 13:04
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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