【问题标题】:iOS: Make layer opaque without fading textiOS:使图层不透明而不褪色文本
【发布时间】:2012-02-03 18:16:01
【问题描述】:

我的 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;
}

【问题讨论】:

    标签: iphone objective-c uiview ios5


    【解决方案1】:

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

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

    此方法也适用于 CALayer。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        使用子视图(与您的 UIView 大小相同)并将子视图上的颜色和 alpha 设置为您想要的透明度,而不是设置视图的 alpha。设置顶层 UIView 的 backgroundColor 为 clearColor。

        【讨论】:

          猜你喜欢
          • 2017-11-20
          • 1970-01-01
          • 1970-01-01
          • 2010-11-19
          • 2013-07-05
          • 1970-01-01
          • 2013-03-26
          • 1970-01-01
          • 2012-03-23
          相关资源
          最近更新 更多