UITableCell 点击 子view背景色消失问题

方式1:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    UIColor *color = self.aLab.backgroundColor;
    [super setSelected:selected animated:animated];
    self.aLab.backgroundColor = color;
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    UIColor *color = self.aLab.backgroundColor;
    [super setHighlighted:highlighted animated:animated];
    self.aLab.backgroundColor = color;
}

方式2:
使用view.layer.backgroundColor

{
        //使用Label.layer.backgroundColor有效✅
        UILabel *view = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 8)];
        view.layer.backgroundColor = [UIColor blueColor].CGColor;
        [self.contentView addSubview:view];
        
        //使用Label.backgroundColor无效❌
        UILabel *view2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 50, 8)];
        view2.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:view2];
        
        //使用Label.backgroundColor无效❌
        UILabel *view3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 50, 8)];
        view3.backgroundColor = [UIColor blueColor];
        view3.layer.backgroundColor = [UIColor redColor].CGColor;
        [self.contentView addSubview:view3];
    }

    {
        //使用View.layer.backgroundColor无效❌
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 30, 50, 8)];
        view.layer.backgroundColor = [UIColor redColor].CGColor;
        [self.contentView addSubview:view];
        
        //使用View.backgroundColor无效❌
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 40, 50, 8)];
        view2.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:view2];
    }

//无色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//蓝色
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
//灰色
cell.selectionStyle = UITableViewCellSelectionStyleGray;

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