Is there a way to set cornerRadius
for only top-left and top-right corner of a UIView
?
I tried the following, but it end up not seeing the view anymore.
UIView *view = [[UIView alloc] initWithFrame:frame];
CALayer *layer = [CALayer layer];
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRoundedRect:frame byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(3.0, 3.0)];
layer.shadowPath = shadowPath.CGPath;
view.layer.mask = layer;
Swift 4
Pay attention to the fact that if you have layout constraints attached to it, you must refresh this as follows in your UIView subclass:
If you don't do that it won't show up.
And to round corners, use the extension:
Here is a Swift version of @JohnnyRockex answer
Note
If you're using Auto Layout, you'll need to subclass your
UIView
and callroundCorners
in the view'slayoutSubviews
for optimal effect.A way to do this programmatically would be to create a
UIView
over the top part of theUIView
that has the rounded corners. Or you could hide the top underneath something.And finally… there is CACornerMask in iOS11! With
CACornerMask
it can be done pretty easy:Try this code,
Swift 4