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

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.

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>

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

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

Related

Blurry CALayer when using inside UIScrollView

I have a CustomView, inside which I have added my custom CALayer ([self.layer addSublayer:...]) which I subclassed to do some custom drawing in drawInContext method of my layer. I draw there simple paths.
The CustomView is added as subview to UIScrollView.
The problem is when I zoom, the content doesn't get redrawn so the path looks blurry.
How to fix that? I tired to call setNeedsDisplay on the layer and sublayer when zoom ends, but without results.
Example of blurry path and correct one after zooming:

Shadow inside UIView

I am working on iPhone application development and have come across shadows of UIView.
I know how to show shadows to a UIView but what I am actually interested in is to drop shadow inside the UIView.
Like when I set shadow properties of a UIView the shadow is dropped behind the view. I want it to come over the view so that the view looks as if it is pressed inside.
Example of such view is UITextField with roundedRect styling.
Thanks,
It depends a lot on the final effect you want to achieve.
The easies way would be a custom image with a prebacked shadow as background. This will give the illusion of a recession in the surface of the view. You can then add subviews to it as usual.
Alternatively, you can override the drawRect: method and draw the view as you like there, "inverted drop shadow" included.

CALayer and UIView display order

I have a CATiledLayer within a UIView and the UIView also contains a subview.
How can I make sure that the subview is always drawn above the layer?
Most of the time I get the tile layer covering the subview.
By default all layers (hence views) added in the last are drawn on the top. You can change the default with -insertSublayer:below: and similar methods:
[view.layer insertSublayer:tiledLayer below:thatSubview.layer]

CALayer and Quartz Transparency

I have a CALayer subclass. I have overridden the drawInContext method. I want the majority of my layer to be transparent except a few areas. I'm using the layer as a menu and I want the icons and labels on the menu to be opaque.
Is it possible to have a CALayer's sublayers be opaque if the super CALayer is transparent?
Is it possible to draw opaque tems in the drawInContext method of a transparent CALayer? I have tried using CGContextSetAlpha, but I assume it doesn't work since the CALayer that I am drawing for is transparent.
As always, any help would be great.
I found out that it is possible. A super layer does not dictate the transparency of its sublayer.

Border image on UIView

I want to have a UIView subclass that has a border image, but I don't want or care about this 'new' frame/bounds around the border image itself.
What I wanted to do was just use drawRect and draw outside of the rect but all drawing is clipped and I don't see a way to not clip drawing outside of this context rect.
So now I have added a sublayer to the views layer, set [self clipsToBounds] on the view and override setFrame to control my sublayers frame and always keep it at the proper size (spilling over the views frame by 40px).
The problem with this is that setFrame on a uiview by default has no animation but seTFrame on a calayer does.
I cant just disable the animations on the calayers setFrame because if I were to call setFrame on the uiview inside a uiview animation block the calayer would still have its animation disabled.
The obvious solution is to look up the current animationDuration on the uiview animation and set a matching animation on the sublayer, but I don't know if this value is available. And even if it is, I'm afraid that calling an animation from within another animation is wrong.
Unfortunately the best solution is to not use a calayer at all and just add a uiview as a subview and draw into that just like I am drawing into my layer, and hope that with autoresizingMask set to height and width that everything will 'just work'. Just seems like unnecessary overhead for such a simple task.
My solution would be to override the initWithFrame: to add the surrounding border pixels and contain the content in a subview. It probably is unneccesary overhead but definietly the "cocoa" route. It's probably going to be easier in the end too since a subview structure will allow you to edit the content seperatly from the border so you dont have to redraw the border when you redraw the content. And keeping them seperate simply makes sense from a OOP perspective.
The clipsToBounds route is probably the easiest route besides the subview structure but managing the border and content in one drawing cycle and in one object will probably be a lot more work so it'll be worth the overhead.
Excuse any typos, typed this from my iPhone.