8

Question I'm struggling on. I've searched SO for a fair good time by now, but couldn't find an answer.

I have a UIView which contains alpha value of 0.5, means - it transparent.

If I'm applying the usual code for UIView shadowing -

 class func applyShadow(view : UIView)
    {
        view.layer.shadowColor = UIColor.blackColor().colorWithAlphaComponent(0.15).CGColor
        view.layer.shadowOpacity = 1
        view.layer.shadowOffset = CGSizeMake(0, 0.5)
        view.layer.shadowRadius = 1.3
        view.layer.masksToBounds = false
        view.layer.shouldRasterize = true
        view.layer.rasterizationScale = UIScreen.mainScreen().scale

    }

The "fill" of the my UIView get shadowed as well.

How can I draw the shadow only on the "border" path of my UIView, excluding the UIView fill?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Roi Mulia
  • 5,626
  • 11
  • 54
  • 105
  • What's the full colour of your view and size of your view. – Joe Oct 31 '16 at 01:57
  • @Joe let's say : frame : (30,30,100,50). BackgroundColor : UiColor.redColor(colorWithAlpha : 0.5) – Roi Mulia Oct 31 '16 at 01:59
  • from your code. your shadow values are very small not visible enough. i believe you trying to make a drop shadow on your view ?. try adding small hairline border to your view with lightColor.it should works. – Joe Oct 31 '16 at 02:21
  • 2
    @Joe Hey! The values are just for this sample. When there is transparency, the Shadow is drawing within the UIView container. I need the same effect, just draw the UIView only to the "border", not including the rest of the UIView – Roi Mulia Oct 31 '16 at 02:25
  • this is now possible: https://stackoverflow.com/a/59092828/294884 – Fattie Nov 25 '22 at 14:12

1 Answers1

0

For 2022 this is now possible.

Essentially you do this:

shadowHole.fillRule = .evenOdd
shadowHole.path = p.cgPath

full details ...

https://stackoverflow.com/a/59092828/294884

Fattie
  • 27,874
  • 70
  • 431
  • 719