UITableViewCell selectedBackgroundView';s颜色在iOS 13上构建时不可见

UITableViewCell selectedBackgroundView';s颜色在iOS 13上构建时不可见,uitableview,ios13,xcode11,Uitableview,Ios13,Xcode11,我已经在cellforrowatinexpath中的选择中为tableview单元格指定了颜色 let backgroundView = UIView() backgroundView.backgroundColor = UIColor.grey3 //custom color cell.selectedBackgroundView = backgroundView 因为我是用Xcode 11.0构建的,所以在iOS 13设备或模拟器上,颜色不再传播到单元格的子视图。如

我已经在
cellforrowatinexpath
中的选择中为tableview单元格指定了颜色

    let backgroundView = UIView()
    backgroundView.backgroundColor = UIColor.grey3 //custom color
    cell.selectedBackgroundView = backgroundView
因为我是用Xcode 11.0构建的,所以在iOS 13设备或模拟器上,颜色不再传播到单元格的子视图。如果我使用Xcode 11.0在iOS 12.2模拟器上构建,它仍然可以工作

有人知道是什么变化导致了这种行为吗?我正在处理.xib文件。

来自苹果的:

当单元格高亮显示或选中时,UITableViewCell类不再更改contentView及其任何子视图的backgroundColor或isOpaque属性。如果在contentView内部(包括)单元格的任何子视图上设置不透明背景色,则单元格高亮显示或选中时的外观可能会受到影响。解决子视图问题的最简单方法是确保其backgroundColor设置为nil或clear,且其不透明属性为false。但是,如果需要,可以替代setHighlighted(:animated:)和setSelected(:animated:)方法,以便在移动到高亮显示和选定状态或从中移动时手动更改子视图上的这些属性

我的快速测试证实这就是你的原因

带有绿色背景标签的单元格,橙色视图为
。已选择BackgroundView

iOS 12:

监督厅13:


我也遇到了同样的问题,我的解决方案是:

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类(我在Cell的ContentView中有一个UIView):


注意:这是我在stackoverflow的第一个答案,请检查它是否正确。

如果您使用层次调试器,您将看到在iOS 13中,
contentView
位于
backgroundView
selectedBackgroundView
之上

这可以通过设置

contentView.backgroundColor = nil 
awakeFromNib中

或者将
contentView
backgroundcolor
设置为在情节提要中清除

contentView.backgroundColor = nil