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

// 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];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容