UISwitch突然将背景颜色从clearColor更改为whiteColor

时间:2019-01-07 04:20:13

标签: ios objective-c uiswitch

所以我有一个UISwitch,在tableViewCell.m中,backgroundColor已经设置为clearColor:

- (instancetype)initWithStyle:(UITableViewCellStyle)style 
reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) 
{

    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor colorWithHexString:@"#333333"];

    [self initUI];
}
return self;
}

- (void)initUI {
[self addSubview:self.topLine];
[self addSubview:self.imgView];
[self addSubview:self.titleLab];
[self addSubview:self.rightView];
[self addSubview:self.rightSwitch];
[self addSubview:self.cellLine];
[self addSubview:self.bottomLine];
}

- (UISwitch *)rightSwitch {
if (!_rightSwitch) {
    self.rightSwitch = [[UISwitch alloc] init];
    self.rightSwitch.frame = CGRectMake(253*kScaleXAndWidth, 8*kScaleYAndHeight, 51*kScaleXAndWidth, 31*kScaleYAndHeight);
    self.rightSwitch.hidden = YES;
    [self.rightSwitch setBackgroundColor:[UIColor clearColor]];
    [self.rightSwitch addTarget:self action:@selector(rightSwitchClick:) forControlEvents:UIControlEventTouchUpInside];
}
return _rightSwitch;
}

rightSwitchClick是一个块,然后在cellForRowAtIndexPath TableViewController.m中:

QuickLoginCell *cell = [tableView dequeueReusableCellWithIdentifier:QuickLoginCellID forIndexPath:indexPath];
cell.rightView.hidden = YES;
cell.rightSwitch.hidden = NO;
__block QuickLoginCell *blockCell = cell;
 if (isIDlogin) {
            [cell.rightSwitch setEnabled:NO];
        }
        else{
            [cell.rightSwitch setEnabled:YES];
        }
        cell.rightSwitch.on = NO;
        cell.bottomLine.hidden = NO;
        if (![BetwayUtils isEmptyString:patternLock]) {
            cell.rightSwitch.on = YES;
            cell.bottomLine.hidden = YES;
        }

        [cell.imgView setImage:[UIImage imageNamed:@"ic_patternLock"]];
  cell.rightSwitchAddClick = ^{
            if (blockCell.rightSwitch.on) {
                PatternLockViewController *vc = [PatternLockViewController new];

                [strongSelf.navigationController pushViewController:vc animated:YES];
            }
            else{

            }
        };

因此,当打开它时,它将直接转到PatternLockViewController,并且在我设置了PatternLock之后,它将再次弹出到TableViewController,并且现在将打开该开关。问题是当我尝试将其关闭时,backgroundColor突然变为白色:

whiteBackground UISwitch

当我删除时:

            PatternLockViewController *vc = [PatternLockViewController new];

            [strongSelf.navigationController pushViewController:vc animated:YES]; 

所以在该块内没有代码,UISwitch backgroundColor是clearColor,我尝试打开和关闭,并且按预期方式工作。所以我对此事有点困惑,因为我没有在任何地方将UISwitch backgroundColor设置为白色。

更新

已经尝试使用委托从patternlockviewcontroller中弹出时刷新表,但仍然无济于事

1 个答案:

答案 0 :(得分:0)

我用以下方法解决它:

- (void)prepareForReuse {
[super prepareForReuse];

[self.rightSwitch setBackgroundColor:[UIColor clearColor]];
[self.rightSwitch setTintColor:[UIColor whiteColor]];
[self.rightSwitch setThumbTintColor:[UIColor whiteColor]];
}

在我的tableViewCell.m上,我希望它能对这里的人有所帮助。