iOS Masonry布局(四) - 视图设置圆角

视图设置任意圆角

@property (nonatomic, strong)UIView *cornerView;

- (UIView *)cornerView {
    if (!_cornerView) {
        _cornerView= [[UIView alloc] initWithFrame:CGRectMake(30.f, 200.f, CGRectGetWidth([[UIScreen mainScreen] bounds])-60.f, 50.f)];
        _cornerView.backgroundColor = [UIColor redColor];
    }
    return _cornerView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     //Do any additional setup after loading the view.
   
    [self.view addSubview:self.cornerView];
    
    UIRectCorner rectCorner = UIRectCornerTopRight | UIRectCornerBottomRight;
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.cornerView.bounds byRoundingCorners:rectCorner cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.cornerView.bounds;
    maskLayer.path = bezierPath.CGPath;
    self.cornerView.layer.mask = maskLayer;
}
圆角.png

Masonry布局视图设置圆角

若使用Masonry布局的视图设置后发现,设置的圆角不起作用。这是因为设置布局后视图并没有立即计算出frame值。需要调用layoutIfNeeded(告知页面布局立刻更新)或在viewDidLayoutSubviews方法中再设置。

layoutIfNeeded

    [self.view addSubview:self.cornerView];
    
    [self.cornerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(30.f);
        make.right.equalTo(self.view.mas_right).offset(-30.f);
        make.height.mas_equalTo(50.f);
        make.top.mas_equalTo(200.f);
    }];
    
    //立即更新布局
    [self.cornerView layoutIfNeeded];

    UIRectCorner rectCorner = UIRectCornerTopRight | UIRectCornerBottomRight;
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.cornerView.bounds byRoundingCorners:rectCorner cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.cornerView.bounds;
    maskLayer.path = bezierPath.CGPath;
    self.cornerView.layer.mask = maskLayer;

viewDidLayoutSubviews

- (void)viewDidLoad {
    [super viewDidLoad];
     //Do any additional setup after loading the view.

    [self.view addSubview:self.cornerView];
    [self.view setNeedsUpdateConstraints];
}
- (void)updateViewConstraints {
    [self.cornerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(30.f);
        make.right.equalTo(self.view.mas_right).offset(-30.f);
        make.height.mas_equalTo(50.f);
        make.top.mas_equalTo(200.f);
    }];
    [super updateViewConstraints];
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    if (nil == self.cornerView.layer.mask && CGRectGetWidth(self.cornerView.frame) >0 ) {
        //该方法会调用多次-如子视图frame改变时
        UIRectCorner rectCorner = UIRectCornerTopRight | UIRectCornerBottomRight;
        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.cornerView.bounds byRoundingCorners:rectCorner cornerRadii:CGSizeMake(8, 8)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.cornerView.bounds;
        maskLayer.path = bezierPath.CGPath;
        self.cornerView.layer.mask = maskLayer;
    }
}

同理,在View控件中进行Masonry布局的子视图设置圆角也需先调用layoutIfNeeded方法或在layoutSubviews方法中在设置。

你可能感兴趣的:(iOS Masonry布局(四) - 视图设置圆角)