3

I've been looking for a way to set the shadow property of a layer, like this: enter image description here

where the light blue is the layer—the UIImageView in this instance—and the dark blue is the shadow.

I'd like the shadow to do things:

  • Appear on all four sides: accomplished by setting shadowOffset to CGSizeMake(0.0, 0.0) and making shadowRadius nonzero
  • Be entirely opaque: accomplished by setting shadowOffset to a specific point in the layer and shadowRadius to zero

I already have shadowOpacityset to zero, but I can't find a way to do both of these. For example, if I have:

imageView.layer.shadowColor = UIColor.orangeColor().CGColor
imageView.layer.shadowOffset = CGSizeMake(4.0 , 4.0)
imageView.layer.shadowOpacity = 1;
imageView.layer.shadowRadius = 0
imageView.layer.masksToBounds = false

I get a solid shadow on two sides. If I want the shadow to be on all four sides, I change shadowOffset to a specific size and shadowRadius to something greater than zero, but then it's not solid.

Randoms
  • 2,110
  • 2
  • 20
  • 31

1 Answers1

1

An alternative option is to use borderWidth and borderColor. Keep in mind that, as mentioned in the docs, borders are drawn inside the layer bounds, rather than extending outside, so you may need to adjust its size as appropriate.

(Depending on your use case, you might consider using UIImageView with insets to make a stretchable image, which may have different performance characteristics if your layer is being resized.)

jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • I may have spoken too soon—therein lies the problem. It's a `UIImageView`, so even if I make it larger to compensate for the space lost, will insets make it so the image isn't cropped? – Randoms Jan 04 '16 at 00:58
  • You might be able to use a combination of the view/layer's contents mode ("center" rather than "fill"/"fit") and adjusting its size. Or, you could prerender a border on the image. You could also try making it a resizable image. – jtbandes Jan 04 '16 at 01:02
  • Do you know how can I change its size? `imageView.image.size` is read-only. – Randoms Jan 04 '16 at 01:12
  • I mean the view/layer's size. – jtbandes Jan 04 '16 at 01:13
  • OK, thanks for your help. I'll look for a way to incorporate those so that the image isn't cropped. Meanwhile, I'll accept this answer. – Randoms Jan 04 '16 at 01:15