UITableViewCell selectedBackgroundView's color not visible when building on iOS 13 - uitableview

I have given a tableview cell a color on selection in cellForRowAtIndexPath using
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.grey3 //custom color
cell.selectedBackgroundView = backgroundView
Since I am building with Xcode 11.0 the color is not propagated to the subviews of the cell anymore on an iOS 13 device or Simulator. If I build on an iOS 12.2 simulator using Xcode 11.0 it still works.
Anyone has an idea what has changed to cause this behaviour? I am working with .xib files.

From Apple's iOS 13 Release Notes:
The UITableViewCell class no longer changes the backgroundColor or isOpaque properties of the contentView and any of its subviews when cells become highlighted or selected. If you are setting an opaque backgroundColor on any subviews of the cell inside (and including) the contentView, the appearance when the cell becomes highlighted or selected might be affected. The simplest way to resolve any issues with your subviews is to ensure their backgroundColor is set to nil or clear, and their opaque property is false. However, if needed you can override the setHighlighted(:animated:) and setSelected(:animated:) methods to manually change these properties on your subviews when moving to or from the highlighted and selected states.
My quick test confirms this would be the cause in your case.
Cell with green-background label, orange view as .selectedBackgroundView.
iOS 12:
iOS 13:

If you use the hierarchy debugger, you'll see that in iOS 13 the contentView sits above the backgroundView and selectedBackgroundView.
This can be resolved by setting
contentView.backgroundColor = nil
in awakeFromNib
or setting the contentView's backgroundColour to clear in the storyboard

I had the same problem, my solutions is:
TableViewController:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "testCell")! as! TestCell
// Turn off selection style for iOS12, iOS11, etc...
cell.selectionStyle = .none
return cell
}
Cell Class (I have a UIView inside cell's ContentView):
class TestCell: UITableViewCell {
#IBOutlet weak var testCellBackgroundView: UIView!
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
contentView.backgroundColor = UIColor.white
testCellBackgroundView.backgroundColor = UIColor.red
} else {
contentView.backgroundColor = UIColor.white
testCellBackgroundView.backgroundColor = UIColor.green // default background color
}
}
// You may change highlighted color of a cell the same way
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
if highlighted {
contentView.backgroundColor = UIColor.white
testCellBackgroundView.backgroundColor = UIColor.red
} else {
contentView.backgroundColor = UIColor.white
testCellBackgroundView.backgroundColor = UIColor.green
}
}
}
Note: this is my first answer at stackoverflow, please check if it's correct.

Related

UITableViewCell editing accessory color

I have a UITableView and have set multiple selection while editing to true
tableView.allowsMultipleSelectionDuringEditing = true
In the cell configuration (cellForRowAtIndexPath), I do:
cell.selectionStyle = .default
cell.accessoryType = .checkmark
So when tableView is set to editing mode, I get blue checkmark option on the left side. I want to change the default blue color on that accessory. How do I do that?
If you're using storyboard :
Change the tint color here
In your custom UITableViewCell, set cell's tintColor in awakeFromNib() like so,
class CustomCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
self.tintColor = .red
}
}
Just set the cell tint color whatever you want. It will automatically change the accessory type color.
[cell setTintColor:[UIColor whiteColor]];

need IOS UI like the picture

enter image description here
a video clip of this UI, how does it look like
Please Help me how can I make one, I tried to build it by UITableView and for cell UIcollectionView it works well but I can't select collection-view Cell by code ( need after select change the cell view background color )
you can change the background color of UICollectionViewCell by overriding the cell property
override var isSelected: Bool {
didSet {
if isSelected {
self.contentView.backgroundColor = UIColor.blue
} else {
self.contentView.backgroundColor = UIColor.green
}
}
}
also since cell are reusable you may see some cells with blue color while scrolling as they are now reused, to avoid this override the method
override func prepareForReuse() {
super.prepareForReuse()
self.contentView.backgroundColor = UIColor.green
}

swift change label on uitableviewcell background color

I have UItablview with custom cell i need to change a label background colour when select this row, but the label colour is repeated when scroll down
You could subclass your Cell like this (and cellForRow then will not be responsible for updating the color, only for setting default color).
class YourTableViewCellClass: UITableViewCell {
#IBOutlet weak var yourLabel: UILabel!
override func setSelected(_ selected: Bool, animated: Bool) {
if(selected) {
self.contentView.backgroundColor = UIColor.red //or what you want as your cell bg color
self.yourLabel.backgroundColor = UIColor.green //or what you want
} else {
self.contentView.backgroundColor = UIColor.white //or what you want as your cell bg color
self.yourLabel.backgroundColor = UIColor.red //or what you want
}
} }
What I understand is, you put code in didSelectRow method of tableview to change the color, but it shows previous color while scrolling.
So,you need to set condition in cellForRow method also e.g.
if(condition)
{
lbl.textcolor = x
}
else
{
lbl.textcolor = y
}

UITableView background color and separator style change not working

I tried to change the background and separator style in a UITableView, but nothing happens. In the snippet below, everything besides those changes works.
This tableView is located in a #IBDesignable UIView. Best part is, that everything shows as it should in the interface builder. The colors change, the separators dissapear.
func getTableViewToDisplayRecords() -> UITableView {
let tableView:UITableView = UITableView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: self.frame.size.width, height: (self.frame.size.height * 3) / 4)), style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView(frame:CGRectZero)
tableView.backgroundColor = UIColor.clearColor()
tableView.separatorStyle = .None
return tableView
}
What could be causing this behavior?
----- EDIT -----
I have managed to do a workaround:
UITableView.appearance().backgroundColor = UIColor.clearColor()
UITableView.appearance().separatorStyle = .None
Now everything works fine, although this is not really a solution of the problem. Any new ideas anyone?
Try the code snippet below to change the background colour of UITableview along with no separator between the table cell's.
{
tableView.backgroundColor = UIColor.clearColor()
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
}
Also, you need to change the background color of UITableViewCells
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
// change the background color of cell, which is by-default white
cell.backgroundColor = UIColor.clearColor()
return cell
}
Here is the screenshot with the table background color being red (for demonstration purpose) and no seperator between cell.
And here is another image with table background being red and single line seperator between cell.
Hope this helps!!
I think this is a Bug in UITableView.
When your tableView move to superview, sometimes the 'separatorStyle' property will reset to UITableViewCellSeparatorStyleSingleLine(the default style).
So you must set this property again when you finish frame change. Like this:
[self.view addSubview:_searchContentView];
_searchContentView.separatorStyle = UITableViewCellSeparatorStyleNone;

Circle View is gone when press UITableViewCell

In UITableViewCell's contentView, I have put a UIView and set its layer cornerRadius to half of its height to make it a circle.And set its color to red.
When I run it and press the UITableViewCell,the red circle becomes transparent.
Before press the cell.
After press the cell.
Where goes wrong,I think it is something to do with cornerRadius.Can anyone help me?
As I have answered here :
UITableViewCell changes the background color of all sub views when
cell is selected or highlighted.
I have adapted my previous answer for your question
You have three options :
If you don't want any selection style
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Subclass UITableViewCell and overriding Tableview cell's setSelected:animated and/or setHighlighted:animated like Leo answered
Add the circle as a layer
CALayer* layer = [CALayer layer];
layer.frame = CGRectMake(10, 10, 30, 30);
layer.backgroundColor = [UIColor redColor].CGColor;
layer.cornerRadius = 20/2.0f;
[cell.contentView.layer addSublayer:layer];
Because UITableviewCell change the view background color when selected.
You need to subclass the UITableviewCell,and change the backgroundcolor back
Screenshot
Code
class CustomCell:UITableViewCell{
override func setHighlighted(highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
let view = self.viewWithTag(10) //The tag of this view is 10
view?.backgroundColor = UIColor.redColor()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
let view = self.viewWithTag(10)
view?.backgroundColor = UIColor.redColor()
}
}

Resources