Now this is iOS 6, everything could change. TheSquad's answer doesn't work for me until I managed to add another line view2.layer.masksToBounds = NO; otherwise the shadow will not be displayed. Although the documentation says that masksToBounds NOT the default, my code shows the opposite.
This is how I make a rounded corner button with a shadow, which is one of the most commonly used code snippets in my application.
button.layer.masksToBounds = YES; button.layer.cornerRadius = 10.0f; view.layer.masksToBounds = NO; // critical to add this line view.layer.cornerRadius = 10.0f; view.layer.shadowOpacity = 1.0f; // set shadow path to prevent horrible performance view.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:_button.bounds cornerRadius:10.0f].CGPath; [view addSubview:button];
EDIT
If the views need to be animated or scrolled, masksToBounds = YES tax performance is significant, which means that the animation is likely to stutter. To get a rounded corner and shadow AND smooth animation or scrolling, use the following code instead:
button.backgroundColor = [UIColor clearColor]; button.layer.backgroundColor = [UIColor redColor].CGColor; button.layer.masksToBounds = NO; button.layer.cornerRadius = 10.0f; view.layer.shadowOpacity = 0.5f; view.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:_button.bounds cornerRadius:10.0f].CGPath; view.layer.shadowOffset = CGSizeMake(0.0f, 4.0f); view.layer.shadowRadius = 2.0f; view.layer.masksToBounds = NO; view.layer.cornerRadius = 10.0f; [view addSubview:button];
Philip007 Mar 23 '13 at 23:54 2013-03-23 ββ23:54
source share