Search code examples
iosobjective-ccocoa-touchios6core-graphics

iOS6 Add Shadow to A Container UIView With A Corner Radius


How can an encompassing UIView have both a shadow and a corner radius at the same time?

I have tried other solutions suggested on SO multiple times, but unfortunately they do not seem to work for iOS6 (or at least not for me)

So I thought I might post this, so that an iOS6 solution can be found.

I have a container UIView which contains two subviews

- a custom UIImageView
- a custom UIView

I would like the entire UIView to have a corner radius of 2.5, but I would also like the UIView to have a shadow. However, so far, I get only 1 of these 2 desires, never both at the same time.

Here is my code, I have different versions of it with my different attempts with SO solutions, but this is just one of my versions.

    self.layer.shouldRasterize = YES;
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    self.layer.cornerRadius = 2.5;
    self.layer.masksToBounds = YES;
    self.layer.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.1].CGColor; //0.1
    self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowRadius = 3.0;

^here self is the containing custom UIView with two subviews described above

Does anyone know of an iOS6 solution to this problem?


UPDATE

So, I do not need a border colour, so I was not adding that when I saw the solutions, but this time I added that, using the solution in the comment below, and it seems that the UIView is getting rounded, but I really want the combined UIImageView and UIView to get rounded.

So basically, the UIImageView is on top, and the UIView is on the bottom.

So how do I get only the top of the UIImageView to be rounded, and only the bottom of the UIView to be rounded.

Thanks.

Note: shadows work as one whole object, but the corner radius is not working as one whole object?


Solution

  • I figured it out.

    self.layer.shouldRasterize = YES;
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    self.layer.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.8].CGColor;
    self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.layer.bounds cornerRadius:self.layer.cornerRadius].CGPath;
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowRadius = 3.0;
    
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [self addSubview:container];
    
    [container addSubview:self.someCustomUIView];
    [container addSubview:self.someCustomImageView];
    
    container.layer.cornerRadius = 2.5;
    container.layer.masksToBounds = YES;
    

    So basically:

    1. I set the shadow of the main UIView.
    2. I created a container subview which contains two other subviews
    3. I set the corner radius of the container subview
    4. Voila! It works!
    5. I hope this works for other people who have multiple subviews in one UIView
    6. I'd like to thank everyone for their help. :)