设置透明度却不影响子视图

// Returns a color in the same color space as the receiver with the specified alpha component.

- (UIColor *)colorWithAlphaComponent:(CGFloat)alpha;

//需要透明度的View

UIView *vagueView = [[UIView alloc] init];

[vagueView setBackgroundColor:

          [[UIColor whiteColor] colorWithAlphaComponent:0.5]];

[self addSubview:vagueView];

[vagueView mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(self.mas_top).offset(50);

make.centerX.mas_equalTo(self.mas_centerX).offset(0);

make.size.mas_equalTo(CGSizeMake(65, 65));

}];

[vagueView.layer setMasksToBounds:YES];

[vagueView.layer setCornerRadius:32.5];

//如果直接设置alpha 会影响到的子视图

UIButton *headBtn = [UIButton buttonWithType:UIButtonTypeCustom];

[headBtn setImage:[ECIMAGENAME(@"user") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  forState:UIControlStateNormal];

[headBtn addTarget:self action:@selector(headerAction:) forControlEvents:UIControlEventTouchUpInside];

[vagueView addSubview:headBtn];

self.headBtn = headBtn;

[headBtn mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(vagueView.mas_top).offset(2.5);

make.centerX.mas_equalTo(vagueView.mas_centerX).offset(0);

make.size.mas_equalTo(CGSizeMake(60, 60));

}];

[_headBtn.layer setMasksToBounds:YES];

[_headBtn.layer setCornerRadius:30];

你可能感兴趣的:(设置透明度却不影响子视图)