UIView的圆角用autolayout裁剪视图

时间:2014-06-04 15:10:22

标签: ios autolayout rounded-corners

我正在使用自动布局启用创建IOS应用。在我的视图应用中,有圆角(仅限顶角)。我正在使用以下功能:

- (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;
}

并在viewDidAppear中调用此函数,如:

-(void)viewDidAppear:(BOOL)animated {

    [self setMaskTo:contentView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight]; 
}

这个工作正常,但问题是当app处于横向模式时,当我从一个视图导航到另一个视图时,它正在裁剪像Portrait视图这样的视图。请告诉我这种方法有什么问题吗?

1 个答案:

答案 0 :(得分:14)

Autolayout不会重新计算您的面具,因此每次布局更改时都必须设置面具。将 setMaskTo:byRoundingCorners: viewDidAppear:移至 viewDidLayoutSubviews:

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self setMaskTo:contentView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
}