Ios UITableViewCell:子视图被其他单元格和表视图外部截断的阴影?

Ios UITableViewCell:子视图被其他单元格和表视图外部截断的阴影?,ios,uitableview,shadow,Ios,Uitableview,Shadow,我有一个垂直间距为10px的电池设计。我想在这些单元格周围设置阴影(实际上我将这些阴影应用于每个单元格的子视图),但我得到的结果如下: 如您所见,阴影被其他单元格和表视图页眉/页脚切断 这是我的密码: 视图控制器 final class SCVC: UIViewController { @IBOutlet weak var tableView: UITableView! { didSet { tableView.backgroundColor =

我有一个垂直间距为10px的电池设计。我想在这些单元格周围设置阴影(实际上我将这些阴影应用于每个单元格的子视图),但我得到的结果如下:

如您所见,阴影被其他单元格和表视图页眉/页脚切断

这是我的密码:

视图控制器

final class SCVC: UIViewController {
    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.backgroundColor = .clear
            tableView.separatorStyle = .none
        }
    }

    override func viewDidLoad() {
        tableView.register(UINib(nibName: "ShadowCell", bundle: .main), forCellReuseIdentifier: "ShadowCell")
        view.backgroundColor = .white
    }
}

extension SCVC: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "ShadowCell", for: indexPath) as! ShadowCell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView(frame: CGRect(origin: .zero,
                                              size: CGSize(width: tableView.bounds.width,
                                                           height: 20)))
        headerView.backgroundColor = .clear
        let label = UILabel(frame: CGRect(origin: CGPoint(x: 16, y: 8),
                                          size: CGSize(width: headerView.frame.width - 32,
                                                       height: 34)))
        label.backgroundColor = .clear
        label.text = "Header view"
        headerView.addSubview(label)
        return headerView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
}
单元格

final class ShadowCell: UITableViewCell {
    @IBOutlet weak var innerView: UIView! {
        didSet {
            innerView.backgroundColor = .white
            innerView.layer.cornerRadius = 10.0
            innerView.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
            innerView.layer.shadowOpacity = 1.0
            innerView.layer.shadowRadius = 24.0
            innerView.layer.shadowOffset = CGSize(width: 0, height: 8)
        }
    }
}
细胞XIB

我怎样才能解决这个问题


感谢您的帮助

一般来说,如果有什么东西在剪辑,可能有以下主要原因:

  • clipsToBounds设置为true
  • 背景颜色不清楚
  • 所以,要解决您的问题,您需要修复单元格及其内容视图的问题。您可以在xib中或在代码中执行此操作:

    cell.clipsToBounds = false
    cell.contentView.clipsToBounds = false
    cell.backgroundColor = .clear
    cell.contentView.backgroundColor = .clear
    

    谢谢你的回答!通过使用debug view hierarchy tool,我意识到了问题所在,UI中唯一没有剪裁的部分是
    单元格.contentView