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

2019-11-14  本文已影响0人  sunflower1518

方式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;
上一篇下一篇

猜你喜欢

热点阅读