Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell makes label's background clear when highlighted

I have a UIlabel on a UITableViewCell, which I've created programmatically (i.e. not a nib or a subclass).

When the cell is highlighted (goes blue) it makes all the background colors of the UILabels turn clear. I have 2 UILabels where I don't want this to be the case. Currently I'm using UIImageViews behind the UILabel's to make it look like the background color doesn't change. But this seems an inefficient way to do it.

How can i stop certain UILabel's background color changing when the UITableViewCell is highlighted?

like image 697
Jonathan. Avatar asked Jun 03 '10 10:06

Jonathan.


4 Answers

You need to subclass UITableViewCell and override the following two methods:

Objective-C:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    UIColor *backgroundColor = self.myLabel.backgroundColor;
    [super setHighlighted:highlighted animated:animated];
    self.myLabel.backgroundColor = backgroundColor;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    UIColor *backgroundColor = self.myLabel.backgroundColor;
    [super setSelected:selected animated:animated];
    self.myLabel.backgroundColor = backgroundColor;
}

Swift

override func setSelected(_ selected: Bool, animated: Bool) {
    let color = myLabel.backgroundColor
    super.setSelected(selected, animated: animated)
    myLabel.backgroundColor = color
}

override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    let color = myLabel.backgroundColor
    super.setHighlighted(highlighted, animated: animated)
    myLabel.backgroundColor = color
}
like image 92
Vahan Avatar answered Nov 10 '22 06:11

Vahan


Another way to keep the background color from changing on highlight is to set the backgroundColor property on the label's layer instead of on the label itself.

#import <QuartzCore/QuartzCore.h>

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // Get a table cell
    UITableViewCell *cell = [tableView dequeueReusableCellForIdentifier:@"cell"];

    // Set up the table cell
    cell.textLabel.text = @"This is a table cell.";

    // If you're targeting iOS 6, set the label's background color to clear
    // This must be done BEFORE changing the layer's backgroundColor
    cell.textLabel.backgroundColor = [UIColor clearColor];

    // Set layer background color
    cell.textLabel.layer.backgroundColor = [UIColor blueColor].CGColor;

    return cell;
}

The layer is not affected by cell highlighting or selection.

like image 45
Tommy Avatar answered Nov 10 '22 06:11

Tommy


Alternatively subclass the label you don't want to change color:

@interface PersistentBackgroundLabel : UILabel {
}

- (void)setPersistentBackgroundColor:(UIColor*)color;

@end


@implementation PersistentBackgroundLabel

- (void)setPersistentBackgroundColor:(UIColor*)color {
    super.backgroundColor = color;
}

- (void)setBackgroundColor:(UIColor *)color {
    // do nothing - background color never changes
}

@end

Then set the color once explicitly using setPersistentBackgroundColor:. This will prevent background color changes from anywhere without using your custom explicit background color change method.

This has the advantage of also eliminating clear background in label during transitions.

like image 41
user479821 Avatar answered Nov 10 '22 05:11

user479821


I may be mistaken, but I couldn't find away to directly override an existing getter/setter in Swift. Building off user479821's answer, I found a workaround that seems to produce the desired results. I added the IBDesignable/IBInspectable annotations in case you use storyboard, which render's the final color in the editor.

@IBDesignable
class PersistentBackgroundLabel: UILabel {
    @IBInspectable var persistentBackgroundColor: UIColor = UIColor.clearColor() {
    didSet {
        super.backgroundColor = persistentBackgroundColor
    }
    }

    override var backgroundColor: UIColor? {
    didSet {
        if backgroundColor != persistentBackgroundColor {
            backgroundColor = persistentBackgroundColor
        }
    }
    }
}
like image 5
Conner Fromknecht Avatar answered Nov 10 '22 07:11

Conner Fromknecht