【问题标题】:TableView Cell view shadow does not coming properly in SwiftTableView 单元格视图阴影在 Swift 中无法正常显示
【发布时间】:2020-05-18 10:04:23
【问题描述】:

我添加了视图作为表格视图单元格的背景,并且我为视图提供了阴影。在这里,当我运行 tableview 时,阴影不会正常出现,一旦我向下和向上滚动,阴影就会正常出现。

我根据这个答案给了阴影answer

代码:

  extension UIView {
  func dropShadow(scale: Bool = true) {
  layer.masksToBounds = false
  layer.shadowColor = UIColor.black.cgColor
  layer.shadowOpacity = 0.5
  layer.shadowOffset = CGSize(width: -1, height: 1)
  layer.shadowRadius = 1
  layer.shadowPath = UIBezierPath(rect: bounds).cgPath
  layer.shouldRasterize = true
  layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PlansTableViewCell

    cell.containerView.dropShadow()
    return cell
}

在像这样滚动阴影之前:

滚动后如下图:

运行后(滚动前)我还需要第二种图像输出,这里帮我写代码。

【问题讨论】:

    标签: swift uitableview uiview shadow


    【解决方案1】:

    问题来了

    layer.shadowPath = UIBezierPath(rect: bounds).cgPath
    

    bounds当时不正确,试试 调用它

    override func layoutSubviews() {
       super.layoutSubviews()
       self.containerViewdropShadow()
    }
    

    cell.layoutIfNeeded()
    cell.containerView.dropShadow()
    

    【讨论】:

    • 你能帮我把阴影放在右侧边缘,就像我在左边一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    相关资源
    最近更新 更多