Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding shadow to top of UIView

How can I add a shadow to the top of my UIView I've tried the following but with no luck...

childView.layer.cornerRadius = 5;
childView.layer.masksToBounds = YES;
childView.layer.shadowOffset = CGSizeMake(-15, 20);
childView.layer.shadowRadius = 5;
childView.layer.shadowOpacity = 0.5;
like image 846
Apollo Avatar asked Jun 09 '14 20:06

Apollo


1 Answers

Swift 3 Extension:

This includes default values for the app I'm working on, but you can change them to match the style you want in your app.

enum VerticalLocation: String {
    case bottom
    case top  
}

extension UIView {
    func addShadow(location: VerticalLocation, color: UIColor = .black, opacity: Float = 0.5, radius: CGFloat = 5.0) {
        switch location {
        case .bottom:
             addShadow(offset: CGSize(width: 0, height: 10), color: color, opacity: opacity, radius: radius)
        case .top:
            addShadow(offset: CGSize(width: 0, height: -10), color: color, opacity: opacity, radius: radius)
        }
    }

    func addShadow(offset: CGSize, color: UIColor = .black, opacity: Float = 0.5, radius: CGFloat = 5.0) {
        self.layer.masksToBounds = false
        self.layer.shadowColor = color.cgColor
        self.layer.shadowOffset = offset
        self.layer.shadowOpacity = opacity
        self.layer.shadowRadius = radius
    }
}
like image 52
Ryan Herubin Avatar answered Oct 14 '22 05:10

Ryan Herubin