iOS:使图层不透明而不淡化文本

时间:2012-02-03 18:16:02

标签: iphone objective-c uiview ios5

我的UIView上有一个带有图层的框架。我让图层设置背景颜色并使alpha为0.5并使frame.backgroundColor = clearColor,以便人们可以看到它背后的线条。然而,它使得持有文本的子视图也逐渐淡出。我该如何防止这种情况?

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

Screenshot

3 个答案:

答案 0 :(得分:3)

要更改视图的透明度,而不是其子视图,可以更改其背景颜色:

myView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.25];

此方法也适用于CALayer。

答案 1 :(得分:1)

你不能在所有uiview上设置alpha值,你必须用alfa设置颜色。

    [self.view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];

答案 2 :(得分:0)

使用子视图(与UIView具有相同的大小)并将子视图上的颜色和alpha设置为所需的透明度,而不是设置视图的alpha。将顶级UIView的backgroundColor设置为clearColor。

相关问题