简体   繁体   English

Swift 带有动态内容的表格视图上的阴影

[英]Swift shadow on tableview with dynamic content

I have a problem with dynamic content with shadow on table view我在表格视图上有阴影的动态内容有问题

I had an extension for make corner radius and make shadow我有一个扩展角半径和阴影

extension UIView {

func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
    let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
    let mask = CAShapeLayer()
    mask.path = path.cgPath

    self.layer.mask = mask

    let shadowLayer = CAShapeLayer()
    shadowLayer.path = path.cgPath
    shadowLayer.frame = self.frame

    shadowLayer.shadowOpacity = 0.4
    shadowLayer.shadowRadius = 2
    shadowLayer.shadowColor = UIColor.black.cgColor
    shadowLayer.shadowOpacity = 0.13
    shadowLayer.shadowRadius = 6
    shadowLayer.shadowOffset = CGSize(width: 0, height: 3)

    self.superview!.layer.insertSublayer(shadowLayer, below: self.layer)
    }
}

And This id MyCustomTableViewCell而这个 id MyCustomTableViewCell

override func awakeFromNib() {
    super.awakeFromNib()

    bg_view.roundCorners([.topLeft, .topRight, .bottomRight, .bottomLeft], radius: 9)
}

And This My result (text is "this is my data on table")这是我的结果(文本是“这是我在表格上的数据”)

我目前的结果

I also use我也用

activity_table.rowHeight = UITableView.automaticDimension
activity_table.estimatedRowHeight = 100

And Set line to 0并将行设置为0

I played around with this some more.我又玩了一些。 You are making this way more complicated than it needs to be.你让这种方式比它需要的更复杂。 I was able to solve this problem without the need for sublayers.我能够在不需要子层的情况下解决这个问题。

class Cell: UITableViewCell {
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var containerView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()
        containerView.layer.cornerRadius = 9
        containerView.layer.shadowColor = UIColor.black.cgColor
        containerView.layer.shadowOpacity = 0.13
        containerView.layer.shadowRadius = 6
        containerView.layer.shadowOffset = CGSize(width: 0, height: 3)
    }
}

containerView is simply a subView that contains the label. containerView 只是一个包含 label 的子视图。 Now, with the containerView, you would adjust the shadowing and the rounded corners.现在,使用 containerView,您可以调整阴影和圆角。

You can check out the full solution here .您可以在此处查看完整的解决方案。

The only thing you need to make sure of is that the containerView's background color is not clear.唯一需要确定的是containerView的背景颜色不清晰。 If so, you are going to see some funky side effects where shadows are propagated to the text label.如果是这样,您将看到一些奇怪的副作用,其中阴影传播到文本 label。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM