UIView with rounded corners and drop shadow?

前端 未结 30 3211
一整个雨季
一整个雨季 2020-11-22 08:00

I’ve been working on an application for a couple of years and received a simple design request: Round the corners on a UIView and add a drop shadow.To do as given below.

30条回答
  •  广开言路
    2020-11-22 08:25

    Something swifty tested in swift 4

    import UIKit
    
    extension UIView {
        @IBInspectable var dropShadow: Bool {
            set{
                if newValue {
                    layer.shadowColor = UIColor.black.cgColor
                    layer.shadowOpacity = 0.4
                    layer.shadowRadius = 1
                    layer.shadowOffset = CGSize.zero
                } else {
                    layer.shadowColor = UIColor.clear.cgColor
                    layer.shadowOpacity = 0
                    layer.shadowRadius = 0
                    layer.shadowOffset = CGSize.zero
                }
            }
            get {
                return layer.shadowOpacity > 0
            }
        }
    }
    

    Produces

    If you enable it in the Inspector like this:

    It will add the User Defined Runtime Attribute, resulting in:

    (I added previously the cornerRadius = 8)

    :)

提交回复
热议问题