Search code examples
iosobjective-cuiswitch

UISwitch suddenly changed background color from clearColor to whiteColor


So I have a UISwitch that backgroundColor already set to clearColor in tableViewCell.m :

- (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 is a block, then in 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{

            }
        };

so when turn on it will directly go to PatternLockViewController, and after I have set the patternLock it will pop to TableViewController again and the switch will be turned on now. the problem is when I try to switch it off the backgroundColor suddenly change to white like this :

whiteBackground UISwitch

When I remove :

            PatternLockViewController *vc = [PatternLockViewController new];

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

so inside the block there is no code and the UISwitch backgroundColor is clearColor, I tried to switch on and off and it works as it supposed to be. so i am a little bit confused on this matter since I dont set UISwitch backgroundColor to white anywhere.

UPDATE

Already tried using delegate to refresh table when pop from patternlockviewcontroller but still no avail


Solution

  • I solve it using :

    - (void)prepareForReuse {
    [super prepareForReuse];
    
    [self.rightSwitch setBackgroundColor:[UIColor clearColor]];
    [self.rightSwitch setTintColor:[UIColor whiteColor]];
    [self.rightSwitch setThumbTintColor:[UIColor whiteColor]];
    }
    

    on my tableViewCell.m I hope it will help someone here.