Solid Shadow in All Directions

2019-07-09 00:24发布

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.

1条回答
The star\"
2楼-- · 2019-07-09 01:06

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.)

查看更多
登录 后发表回答