coderex
coderex

Reputation: 61

Bottom Corner Radius and shadow only at bottom ofUIView in Objective C

I want to add radius to bottom left and bottom right corners of an UIView and then drop shadow only at bottom of the same UIView. I have gone through solutions in which all corners are provided with radius and then shadow. That is working fine. But when I use UIBeizerPath to add radius to bottom corners the shadow property doesn't seem to work. I am using Objective-C and XCode 8.1. How can I do it?

Using below code bottom corners get their radius but shadow properties doesn't work.

UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];

[testView setBackgroundColor:[UIColor yellowColor]];

// shadow
testView.layer.shadowColor = [UIColor colorWithRed:156.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1.0f].CGColor;
testView.layer.shadowOffset = CGSizeMake(0.0f, 2.0f);
testView.layer.shadowRadius = 4.0f;
testView.layer.shadowOpacity = 0.5f;

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0.0, 0.0)];
[path addLineToPoint:CGPointMake(0.0, CGRectGetHeight(testView.frame))];
[path addLineToPoint:CGPointMake(CGRectGetWidth(testView.frame), CGRectGetHeight(testView.frame))];
[path addLineToPoint:CGPointMake(CGRectGetWidth(testView.frame), 0.0)];
testView.layer.shadowPath = path.CGPath;

//bottom corners radius
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:testView.bounds        
                                               byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)         
                                                     cornerRadii:CGSizeMake(2.0, 2.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path  = maskPath.CGPath;
testView.layer.mask = maskLayer;

Upvotes: 2

Views: 592

Answers (1)

Jon Rose
Jon Rose

Reputation: 8563

The mask is masking the shadow. You need to have two view one inside the other. Apply the mask to the inner view and the shadow to the outer view.

Upvotes: 1

Related Questions