There was a time not long ago when I wanted to put drop shadows on almost everything in iOS because I thought it was cool and I wasn’t seeing a lot of it. I assumed that I’d have to use PNGs and some complicated code to make things look good on dynamically sized things I was using.
After a bit I decided to stop pursuing the idea as it wasn’t really needed and the Googling I had done was proving either fruitless or very complicated for my level of understanding at that time.
I’ve been making my own UIViews that serve as sliding panels and to provide some visual separation from content beneath I was going to implement a PNG shadow and then I thought perhaps I should look into things again so that I wasn’t needlessly burning PNG(s) into my project for the effect.
Turns out it’s a whole lot easier than I originally discovered. CALayers are your best friend! Make sure you’ve added QuartzCore and you’re good to go:
foo.layer.shadowColor = [[UIColor blackColor] CGColor]; foo.layer.shadowOffset = CGSizeMake(0, 4); foo.layer.shadowRadius = 5.0; foo.layer.shadowOpacity = 0.4; foo.layer.cornerRadius = 10;
It’s important to note that if you set the view to clip subviews, you won’t see your shadow. I’ve applied this technique to a few items in my application start up and it adds a nice bit of refinement without resorting to bitmap tricks.