Iphone iOS:使图层不透明而不褪色文本
我的UIView上有一个带有图层的框架。我让图层设置背景色,使alpha为0.5,并使frame.backgroundColor=clearColor,以便人们可以看到它后面的线条。但是,它会使包含文本的子视图也淡出。我如何防止这种情况Iphone iOS:使图层不透明而不褪色文本,iphone,objective-c,uiview,ios5,Iphone,Objective C,Uiview,Ios5,我的UIView上有一个带有图层的框架。我让图层设置背景色,使alpha为0.5,并使frame.backgroundColor=clearColor,以便人们可以看到它后面的线条。但是,它会使包含文本的子视图也淡出。我如何防止这种情况 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setAlpha:kAlpha];
CALayer *layer = [self layer];
[layer setMasksToBounds:YES];
[layer setBackgroundColor:[UIColor redColor].CGColor];
[layer setCornerRadius:kCornerRadius];
}
return self;
}
- (id)init
{
if (self = [super init]) {
self.clipsToBounds = YES;
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = NO;
tileTitle = [[UILabel alloc] init];
tileTitle.textColor = [UIColor blackColor];
tileTitle.backgroundColor = [UIColor clearColor];
tileTitle.font = [UIFont boldSystemFontOfSize:13.0f];
tileDescription = [[UILabel alloc] init];
tileDescription.textColor = [UIColor blackColor];
tileDescription.backgroundColor = [UIColor clearColor];
tileDescription.font = [UIFont systemFontOfSize:11.0f];
tileDescription.lineBreakMode = UILineBreakModeTailTruncation;
[self addSubview:tileTitle];
[self addSubview:tileDescription];
}
return self;
}
使用子视图(与UIView大小相同)并将子视图上的颜色和alpha设置为所需的透明度,而不是设置视图的alpha。将顶级UIView的backgroundColor设置为clearColor。使用子视图(与UIView大小相同),将子视图上的颜色和alpha设置为所需的透明度,而不是设置视图的alpha。将顶级UIView的backgroundColor设置为clearColor。要更改视图(而不是其子视图)的透明度,可以更改其背景色:
myView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.25];
此方法也适用于CALayer。要更改视图的透明度,但不更改其子视图,可以更改其背景色:
myView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.25];
此方法也适用于CALayer。您不能在所有uiview上设置alpha值,必须使用alpha设置颜色
[self.view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
不能在所有uiview上设置alpha值,必须使用alfa设置颜色
[self.view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];