1

I'm using the method described at How do I draw a shadow under a UIView? to draw shadow behind a view's content. The shadow is clipped to the view's bounds, although I disabled "Clip Subviews" in Interface Builder for the view. Is it possible to draw a shadow around a view and not only in a view?

I don't want to draw the shadow inside the view because the view would receive touch events for the shadow area, which really belongs to the background.

Community
  • 1
  • 1
Christian
  • 1,027
  • 1
  • 13
  • 26

3 Answers3

6

Instead of manual drawing in drawRect, consider setting properties on the the UIView's Core Animation layer for drawing a shadow.

For example:

[descriptionInput setClipsToBounds:NO];
[descriptionInput.layer setShadowColor:[[UIColor blackColor] CGColor]];
[descriptionInput.layer setShadowOpacity:0.8];
[descriptionInput.layer setShadowOffset:CGSizeMake(0.0, 3.0)];

For this to work, you need to include QuartzCore:

#import <QuartzCore/QuartzCore.h>
Joubert Nel
  • 3,154
  • 3
  • 26
  • 24
2

clipsToBounds only controls the clipping for children of a view, not drawing of that view itself; hence it's not solving your problem.

If you can draw your shadow onto a different view and add that as a child, it won't get clipped. However, I don't know how possible that is with the method you're using :(

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
0

It is not encouraged to draw outside view bounds. Maybe you can include the shadow directly in your background...

Regards,

Quentin
  • 1,741
  • 2
  • 18
  • 29
  • 3
    Why is it not encouraged to draw outside view bounds? In case of shadows, specifically, any reasons? – Raj Pawan Gumdal Oct 16 '12 at 09:56
  • it cause performance issue, because of blending colors between two layers, background layer and shadow layer, its cpu intensive. https://stackoverflow.com/a/36794256/1348121 – Hashem Aboonajmi Aug 30 '17 at 10:31