pixelfreak
pixelfreak

Reputation: 17834

UILabel background obscured when UITableViewCell is selected

I have some UILabel inside a UITableViewCell with opaque background color and white text. When this cell is selected, the UILabel's bg color seems to be obscured by the blue bg color of the selected cell. The UILabel text, however, is showing fine. How do I get the background color of the UILabel to show on top of the cell selection color?

enter image description here

Upvotes: 9

Views: 5031

Answers (11)

zsteed
zsteed

Reputation: 97

I know there are a bunch of answers on this old question. But to confirm the answer listed above works in iOS 10 here it is in swift.

override func layoutSubviews() {
 super.layoutSubviews()
 someLabel.backgroundColor = UIColor.orange
}

Upvotes: 0

k06a
k06a

Reputation: 18745

This one is shorter:

@interface MyLabel : UILabel
@end
@implementation MyLabel
- (void)setBackgroundColor:(UIColor *)backgroundColor {
    if (self.backgroundColor == nil) {
        [super setBackgroundColor:backgroundColor];
    }
}
@end

Sets background color once while awaking from Nib

Upvotes: 1

user3739679
user3739679

Reputation: 11

@implementation CustomCell

//这样就行了啊。。

-(NSArray*)subviews{

    return nil;

}

@end

Upvotes: 1

Pavel
Pavel

Reputation: 652

You can set background color in

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Worked for me.

Upvotes: 0

lexa
lexa

Reputation: 424

Looks like the background color of labels is changed together with the background color of a row (animated). I just subclassed the UILabel and "protected" backgroundColor changes after I set the initial color:

@interface MyLabel : UILabel {
    BOOL isColorLocked;
}
@property (assign, nonatomic) BOOL isColorLocked;
@end

@implementation MyLabel
@synthesize isColorLocked;
- (void)setBackgroundColor:(UIColor *)backgroundColor {
    if (!isColorLocked) {
        super.backgroundColor = backgroundColor;
    }
}
@end

Upvotes: 5

mjisrawi
mjisrawi

Reputation: 7936

Have you considered setting the cell selection style to none and overriding the cell's setHighlighted:(BOOL)highlighted method in a UITableViewCell subclass for better control? Reimplementing the blue background shouldn't be too difficult.

You can just set the cell view's background to blue (solid or a gradient) if highlighted = YES.

Upvotes: 1

jdrama418
jdrama418

Reputation: 216

Had this same issue in an multiple selection table view. Try setting the background color for those labels in -layoutSubviews. Worked for me.

Upvotes: 16

Sam Jarman
Sam Jarman

Reputation: 7377

I have another hunch about this one, since you're setting the background color, and the cell is in the background of the view, and is a subview of that view.. there could be some weirdness going on there.

Upvotes: 0

Sam Jarman
Sam Jarman

Reputation: 7377

Try bringing your label to the front of the view.. you can do this by going to your custom cell .xib, clicking the label and going Editor, Arrangement, Bring to front.

To do that in code, its a little method.. Programmatically send to front/back elements created from interface builder

Upvotes: 0

Dileep Reghu
Dileep Reghu

Reputation: 879

remove selection color using

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Upvotes: 0

Related Questions