This question has been asked few times but there don't seem to be any solution to this.
UITableView reorder hides background
Subviews of UITableViewCell are not visible while reordering
Reordering causing subview 's backround color to clear
I have a custom tableview cell with UILabel in it. When tableview is in edit mode and I drag the cell to reorder, UILabel's background becomes clear color. I've also found that, if I try to reorder selected cell (my tableview is allowed multiple selection during edit mode), the subview's background color stays.
I tried below methods in my CustomCell but none of them overrides the subview's background color when cell is being dragged.
I want the subview's background color to stay. Is there any method that I missed? Or Apple designed it this way?
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
if (self.isEditing) {
self.customLabel.backgroundColor = [UIColor blueColor];
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
if (self.isEditing) {
self.customLabel.backgroundColor = [UIColor blueColor];
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing) {
self.customLabel.backgroundColor = [UIColor blueColor];
}
else {
self.customLabel.backgroundColor = [UIColor blueColor];
}
}
Adding a modern solution for Swift 3.
Create a new .swift file and make a custom UIView class:
import UIKit
class NeverClearView: UIView {
override var backgroundColor: UIColor? {
didSet {
if backgroundColor?.cgColor.alpha == 0 {
backgroundColor = oldValue
}
}
}
}
In Interface Builder, go to the Identity Inspector and set the class to your shiny new NeverClearView class. This will halt it from disappearing during cell reordering.
This solution also works for other UIKit components, for example I had to do the same thing for a UIStepper a second ago.
You may create your custom UILabel. And overload -drawRect.
@interface VALAbel:UILabel
@property (nonatomic, strong) UIColor *bac_color;
@end
@implementation VALabel
-(void)setBac_color:(UIColor *)bac_color
{
_bac_color = bac_color;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[self.bac_color set];
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), true);
CGContextFillRect(UIGraphicsGetCurrentContext(), self.bounds);
[super drawRect:rect];
}
@end
This will help you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With