UIView draw shadow beyond view's frame
I have a requirement where I have to draw a view's shadow beyond the view's frame. We already have complex view hierarchy / layering and it would not be possible now to change the view's frames and layout to accommodate the shadows within the viewController's view's bounds.
1. I am bit concerned here of whether I am allowed to draw the shadow's outside the view by following this methodology:
"You can also create a drop shadow that will be based on the alpha component of whatever is drawn in your view. Often this will result in a shadow just around the edges of the view. This example code on a UILabel:
label.layer.shadowColor = [UIColor blackColor].CGColor; label.layer.shadowOpacity = 1.0; label.layer.shadowRadius = 5.0; label.layer.shadowOffset = CGSizeMake(0, 3); label.clipsToBounds = NO;
In this case, you need clipsToBounds to be NO in order for a shadow outside the frame of your view to show up. Next I’ll show you how you can actually combine rounded corners and drop shadows, since I’m sure that’s what you really want to do now."
2. Now, I have come across threads which tells that drawing shadows outside the view is not encouraged. But no explanation given: How can I draw a shadow beyond a UIView's bounds?
Is there any reason as why we should not be drawing shadows (CALayer) outside view's frame? Or, is it fine following the 1st approach?
I see absolutely no reason not to draw a shadow this way, and this is frankly the only way. Why would you "not be allowed" to draw a shadow outside the bounds? That's what the clipsToBounds property is for.