Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIVIew Corner Radius and Shadows?

Tags:

uiview

In a magazin I read about some nice extensions for the UIView class. You will be able to add a border with corner radian or a drop shadow to any UIView.

@implementation UIView (Extentions)

-(void) enableRoundRectsWithValue:(float)value
{
    self.layer.masksToBounds = true;
    self.layer.cornerRadius = value;
}

-(void) enableShadow
{
    self.layer.masksToBounds = false;
    self.layer.shadowOffset = CGSizeMake(0,2);
    self.layer.shadowOpacity = 0.5;
}
@end

While these methods work fine for themselves they don't play nice together. I can't have a corner radius and a shadow. At least not like you expect them to be. I guess because masksToBounds is set to true in one method and false in the other.

How can I get a UIView with a corner radius and also a shadow (with the same corner radius)?

like image 702
TalkingCode Avatar asked Oct 04 '11 19:10

TalkingCode


1 Answers

It's kind of old but more people with the same problem can get here looking for a solution.

I guess this post can help. It explains a little about CALayer and about mixing effects, including corner radius and shadows.

like image 117
Alécio Gomes Avatar answered Sep 28 '22 09:09

Alécio Gomes