Creating a shadow for a UIImageView that has rounded corners?

前端 未结 5 1410
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 14:17

I am trying to create an ImageView that has rounded corners and a shadow to give it some depth. I was able to create a shadow for the UIImageView,

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 15:09

    Swift 5:

    You can use the below extension:

    extension UIImageView {
        func applyshadowWithCorner(containerView : UIView, cornerRadious : CGFloat){
            containerView.clipsToBounds = false
            containerView.layer.shadowColor = UIColor.black.cgColor
            containerView.layer.shadowOpacity = 1
            containerView.layer.shadowOffset = CGSize.zero
            containerView.layer.shadowRadius = 10
            containerView.layer.cornerRadius = cornerRadious
            containerView.layer.shadowPath = UIBezierPath(roundedRect: containerView.bounds, cornerRadius: cornerRadious).cgPath
            self.clipsToBounds = true
            self.layer.cornerRadius = cornerRadious
        }
    }
    

    How to use:

    1. Drag a UIView on the storyboard
    2. Drag an ImageView inside that UIView

    Storyboard should look like this:

    1. Create IBOutlet for both Views, call extension on your ImageView, and pass above created UIView as an argument.

    Here is the output :

提交回复
热议问题