Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 807
Samuël Avatar asked Sep 25 '19 18:09

Samuël


People also ask

What is a backgroundview in uitableviewcell?

The view to use as the background for a selected cell. UITableViewCell adds the value of this property as a subview only when the cell has a selected state. It adds the selected background view as a subview directly above the background view ( backgroundView) if it isn’t nil, or behind all other views.

What are the cells of UITableView?

The cells of UITableView are instances of UITableViewCell or its subclasses. It is the table view that adds, removes, and arranges cells in its view hierarchy. Table views reuse cells that go out of the screen to display new elements, so that:

What is a uitableviewcell in Salesforce?

A UITableViewCell object is a specialized type of view that manages the content of a single table row. You use cells primarily to organize and present your app’s custom content, but UITableViewCell provides some specific customizations to support table-related behaviors, including: Applying a selection or highlight color to the cell.

Should I use a scroll view or a UITableView?

For example, many developers make their life harder using a scroll view when a UITableView would be a better choice. Finally, architecture is crucial for table views. The code of the table view data source often ends inside view controllers when it should go into a separate class.


3 Answers

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.

like image 157
feheradi Avatar answered Oct 09 '22 08:10

feheradi


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:

enter image description here

iOS 13:

enter image description here

like image 20
DonMag Avatar answered Oct 09 '22 08:10

DonMag


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

like image 30
Ashley Mills Avatar answered Oct 09 '22 10:10

Ashley Mills