Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounded corners of UIView with autolayout cropping the view

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?

like image 216
Uttam Kadam Avatar asked Jun 04 '14 15:06

Uttam Kadam


People also ask

How do you set corner radius for Uiview in storyboard?

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)


1 Answers

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];
}
like image 94
dscastillo171 Avatar answered Sep 21 '22 15:09

dscastillo171