Created
January 22, 2013 19:52
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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