I am creating IOS app with autolayout enable. In my app for views there are rounded corners (top corners only). I am using the following function:
- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}
and calling this function in viewDidAppear like:
-(void)viewDidAppear:(BOOL)animated {
[self setMaskTo:contentView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
}
This is working fine, but the issue is when app is in landscape mode and when I am navigating from one view to another, it is cropping the view like Portrait view. Please let me know is there any issue with this approach?
Select the view that you want to round and open its Identity Inspector. In the User Defined Runtime Attributes section, add the following two entries: Key Path: layer. cornerRadius , Type: Number, Value: (whatever radius you want)
Autolayout won't recalculate your mask, so you will have to set the mask each time your layout changes. Move setMaskTo:byRoundingCorners: from viewDidAppear: to viewDidLayoutSubviews:
- (void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[self setMaskTo:contentView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With