How can I draw a shadow beyond a UIView's bounds?

喜你入骨 提交于 2019-12-06 13:57:37

问题


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.


回答1:


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

Regards,




回答2:


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>



回答3:


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 :(



来源:https://stackoverflow.com/questions/2817261/how-can-i-draw-a-shadow-beyond-a-uiviews-bounds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!