2

So I have an UIView (called myView) with some mask applied to it.

let maskPath = UIBezierPath(roundedRect: myView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 12, height: 12))
let maskLayer = CAShapeLayer()
maskLayer.frame = myView.bounds
maskLayer.path = maskPath.cgPath
myView.layer.mask = maskLayer

Which layout this way:

enter image description here

What I am failing to do is add some shadow to myView. Since the view's layer have a mask, I am not able to add differents layers to it with shadow.

Does anyone ever had this problem?

Gabriel Oliva
  • 143
  • 1
  • 11

1 Answers1

3

set your image color to clear and to the following:

let maskPath = UIBezierPath(roundedRect: redView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 12, height: 12))
        let maskLayer = CAShapeLayer()[![enter image description here][1]][1]
        maskLayer.fillColor = UIColor.red.cgColor // your color
        maskLayer.frame = redView.bounds
        maskLayer.path = maskPath.cgPath

        redView.layer.addSublayer(maskLayer)

        redView.layer.shadowOffset = CGSize(width: 10, height: 10)
        redView.layer.shadowColor = UIColor.green.cgColor
        redView.layer.shadowOpacity = 1
Denys
  • 114
  • 1
  • 6
  • Hey Denys! Adding the `maskLayer` as a sublayer is not rounding the corners. Your code is layouting my view like this: https://imgur.com/a/1ygV1 – Gabriel Oliva Dec 06 '17 at 13:09
  • did you set UIVeiw background color to clear? this the effect: https://imgur.com/gallery/a2Xbx – Denys Dec 06 '17 at 13:47