1

I need to group Header, Cell and Footer in a Shadow, So have create container views and given shadow to these subviews but a thin line gap between them is visible even I have set TableView.separatorStyle = .none

Please see below Image for the same: enter image description here

Below is the method for creating shadow on views

class ShadowView: UIView {
    override var bounds: CGRect {
        didSet {
            setupShadow()
        }
    }

    private func setupShadow() {
        self.layer.masksToBounds = false
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.shadowOpacity = 2.2
        self.layer.shadowOffset = CGSize(width: -1, height: 1)
        self.layer.shadowRadius = 3

        self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
        self.layer.shouldRasterize = true

        self.layer.rasterizationScale = UIScreen.main.scale
    }
}
Yannick Loriot
  • 7,107
  • 2
  • 33
  • 56
Pankaj Bhardwaj
  • 2,081
  • 1
  • 17
  • 37
  • Please comment the code of setupShadow() than check – Saurabh Jain Jan 15 '19 at 12:32
  • You need to add a "shadow view" behind the cells. This may give you what you want (it's Obj-C, but pretty straight-forward): https://stackoverflow.com/a/8706586/6257435 – DonMag Jan 15 '19 at 13:06

1 Answers1

1

Please try this:

Add the shadow to tableViewCell not each view of cell.

if you want a global method please write this:

extension UIView {

func addShadow(cornerRadius: CGFloat, opacity: Float, radius: CGFloat, offset: (x: CGFloat, y: CGFloat)){
    self.clipsToBounds = true
    self.layer.cornerRadius = cornerRadius
    self.layer.masksToBounds = false
    self.layer.shadowColor = UIColor.gray.cgColor
    self.layer.shadowOpacity = opacity
    self.layer.shadowOffset = CGSize(width: offset.x, height: offset.y)
    self.layer.shadowRadius = radius
}

}

then in your tableViewCell class please call this method like:

 override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    self.addShadow(cornerRadius: 8, opacity: 1, radius: 5, offset: (x: 0, y: 0))
 }

Change the value according to your requirement.

It may helps you. Thank you

Sanjukta
  • 1,057
  • 6
  • 16