Skip to content

Instantly share code, notes, and snippets.

@gjritter
Created January 22, 2013 19:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gjritter/4597854 to your computer and use it in GitHub Desktop.
Save gjritter/4597854 to your computer and use it in GitHub Desktop.
The setSelected:animated: and setHighlighted:animated: methods of UITableViewCell appear to make all of the selected background view's subviews transparent. This is annoying when the selected background view contains views that you intend to be non-transparent (like the custom dividers in this example). It necessitates having to add the colors b…
- (id) initWithCoder:(NSCoder*) coder {
if (self = [super initWithCoder:coder]) {
self.backgroundView = [self createBackgroundViewWithBackgroundColor:[UIColor clearColor]];
self.selectedBackgroundView = [self createBackgroundViewWithBackgroundColor:[UIColor colorWithWhite:57.0f/255.0f alpha:1.0f]];
}
return self;
}
- (UIView*) createBackgroundViewWithBackgroundColor:(UIColor*) backgroundColor {
UIView* backgroundView = [[UIView alloc] initWithFrame:self.bounds];
backgroundView.backgroundColor = backgroundColor;
UIView* lightDivider = [[UIView alloc] initWithFrame:CGRectMake(0.0f, self.bounds.size.height - 1.0f, self.bounds.size.width, 1.0f)];
lightDivider.backgroundColor = [UIColor colorWithWhite:75.0f/255.0f alpha:1.0f];
[backgroundView addSubview:lightDivider];
UIView* darkDivider = [[UIView alloc] initWithFrame:CGRectMake(0.0f, self.bounds.size.height - 2.0f, self.bounds.size.width, 1.0f)];
darkDivider.backgroundColor = [UIColor colorWithWhite:37.0f/255.0f alpha:1.0f];
[backgroundView addSubview:darkDivider];
return backgroundView;
}
- (void) setSelected:(BOOL) selected animated:(BOOL) animated {
[super setSelected:selected animated:animated];
if (selected) {
UIView* lightDivider = [[self.selectedBackgroundView subviews] objectAtIndex:0];
lightDivider.backgroundColor = [UIColor colorWithWhite:75.0f/255.0f alpha:1.0f];
UIView* darkDivider = [[self.selectedBackgroundView subviews] objectAtIndex:1];
darkDivider.backgroundColor = [UIColor colorWithWhite:37.0f/255.0f alpha:1.0f];
}
}
- (void) setHighlighted:(BOOL) highlighted animated:(BOOL) animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
UIView* lightDivider = [[self.selectedBackgroundView subviews] objectAtIndex:0];
lightDivider.backgroundColor = [UIColor colorWithWhite:75.0f/255.0f alpha:1.0f];
UIView* darkDivider = [[self.selectedBackgroundView subviews] objectAtIndex:1];
darkDivider.backgroundColor = [UIColor colorWithWhite:37.0f/255.0f alpha:1.0f];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment