【问题标题】:UITableViewCell selectedBackgroundView's color not visible when building on iOS 13在 iOS 13 上构建时,UITableViewCell selectedBackgroundView 的颜色不可见
【发布时间】:2019-09-25 18:24:25
【问题描述】:

我在cellForRowAtIndexPath 中使用

为表格视图单元格指定颜色
    let backgroundView = UIView()
    backgroundView.backgroundColor = UIColor.grey3 //custom color
    cell.selectedBackgroundView = backgroundView

由于我使用 Xcode 11.0 构建,因此颜色不再传播到 iOS 13 设备或模拟器上的单元格子视图。如果我使用 Xcode 11.0 在 iOS 12.2 模拟器上构建它仍然可以工作。

有人知道导致这种行为的原因是什么?我正在处理 .xib 文件。

【问题讨论】:

    标签: uitableview ios13 xcode11


    【解决方案1】:

    来自苹果的iOS 13 Release Notes

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

    我的快速测试证实这就是你的情况。

    带有绿色背景标签的单元格,橙色视图为.selectedBackgroundView

    iOS 12:

    iOS 13:

    【讨论】:

      【解决方案2】:

      如果您使用层次结构调试器,您会看到在 iOS 13 中 contentView 位于 backgroundViewselectedBackgroundView 之上。

      这可以通过设置解决

      contentView.backgroundColor = nil 
      

      awakeFromNib

      或将contentViewbackgroundColour 设置为在情节提要中清除

      【讨论】:

        【解决方案3】:

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

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

        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
                }
            }   
        }
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-18
          • 1970-01-01
          • 2013-11-06
          相关资源
          最近更新 更多