How can I make a custom sublayer in a UITableViewCell
disappear in setSelected:animated:
?
Context:
I've added a custom background layer to my UITableViewCell
by adding a CAGradientLayer
inside of drawRect:
, like so:
- (void)drawRect:(CGRect)rect{
[super drawRect:rect];
[[self providerLabel] setTextColor:kOUBlue];
[self addGrayLayer];
}
- (void)addGrayLayer{
[[[self contentView] layer] insertSublayer:[self grayGradientLayer] atIndex:0];
}
- (CAGradientLayer *) grayGradientLayer{
if (!_grayGradientLayer) {
CAGradientLayer *gradient = [CAGradientLayer layer];
UIColor *white = [UIColor colorWithWhite:1 alpha:2];
UIColor *gray = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
[gradient setColors:@[(id)white.CGColor, (id)gray.CGColor]];
[gradient setFrame:[self bounds]];
_grayGradientLayer = gradient;
}
return _grayGradientLayer;
}
When the user taps on a cell, the blue highlighting doesn't appear. So I've attempted to hide the CAGradientLayer
in setSelected:animated:
like so:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (selected) {
[[self grayGradientLayer] setHidden:YES];
}else{
[[self grayGradientLayer] setHidden:NO];
}
[[self contentView] setNeedsDisplay];
}
The blue background appears, but not until the push animation begins. Is there any way for me to make the default blue appear immediately? I've also tried to add a second colored CAGradientLayer
and swap them manually, but it seems to me that the cell doesn't redraw immediately after I hide the layer in setSelected:
.
I've tried to force the cell to redraw, using setNeedsDisplay
with no luck. Any ideas?