9

I'm trying to create a shadow around a simple UIView object which is added on top of a UIViewController's view. what's the most straight forward way of doing this?

zumzum
  • 17,984
  • 26
  • 111
  • 172

2 Answers2

28

First, be sure to import the Quartz Core library:

#import <QuartzCore/QuartzCore.h>

Next, add the following lines to set up the shadow's properties:

someView.layer.shadowColor = [[UIColor blackColor] CGColor];
someView.layer.shadowOffset = CGSizeMake(10.0f,10.0f);
someView.layer.shadowOpacity = .5f;
someView.layer.shadowRadius = 10.0f;

Keep in mind that if you have that view's clipsToBounds property set to YES, the shadow won't appear.

Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
Ned
  • 6,280
  • 2
  • 30
  • 34
  • Right on. Thanks! I tried that but I couldn't figure out why it never worked and indeed the problem was the clipsToBounds property set to YES. One more question is what if I want the shadow to be on all 4 sides of the View? I think this only lets you have 2 sides at the time by changing the shadowOffset... Anyway, Thanks again! – zumzum Feb 05 '11 at 21:35
  • 1
    If both the offset and the radius are 10, the shadow is hidden behind someView. If you want the shadow on all 4 sides of someView, change the radius to be greater than the offset (or change the offset to 0 to have an even shadow around someView). – FuePi Dec 19 '11 at 13:48
3

It took me some time to figure it out. Code works perfect but you should import quartz

#import <QuartzCore/QuartzCore.h>
doozMen
  • 690
  • 1
  • 7
  • 14