0

I'm trying to put a box shadow on a table view in my iOS app. I tried different techniques for this, but have not yet managed to fix it. I've tried to add the shadow on the layer of the cell itself first :

cell.layer.shadowColor = UIColor.black.cgColor
cell.layer.shadowOpacity = 1
cell.layer.shadowOffset = CGSize.zero
cell.layer.shadowRadius = 2

This didn't work, so I tried to put box shadow on the content view :

cell.contentview.layer.shadowColor = UIColor.black.cgColor
cell.contentview.layer.shadowOpacity = 1
cell.contentview.layer.shadowOffset = CGSize.zero
cell.contentview.layer.shadowRadius = 2

I also tried to add an extra view with the same constraints in the table view cell and put the box shadow on that view, but this didn't change the result. result with extra view and constraints and cornenradius

Something special in the table view is the following :

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section != 0 {
        return 10
    } else {
        return 0
    }
}

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! UITableViewHeaderFooterView
    header.tintColor = UIColor.init(netHex: 0xF9FAFD)
}

The table view data is all put in seperate sections, and 1 row per section.

Thomas L
  • 320
  • 3
  • 14

1 Answers1

3

According to me you should do following:

1) Wrap all UITableviewCell Views in a containerView.

2)Give margins to that parent containerView like 10px from each side.

3)Then apply

cell.containerView.layer.shadowColor = UIColor.black.cgColor
cell.containerView.layer.shadowOpacity = 1
cell.containerView.layer.shadowOffset = //any offset
cell.containerView.layer.shadowRadius = 2

Note: Rasterize cell layers contents to avoid redrawing it always

cell.layer.shouldRasterize = true
ankit
  • 3,537
  • 1
  • 16
  • 32
  • How would I add the margins to the containerview? I have this code at the moment : cell.backView.layer.shadowColor = UIColor.black.cgColor cell.backView.layer.shadowOpacity = 1.0 cell.backView.layer.shadowOffset = CGSize.zero cell.backView.layer.shadowRadius = 2.0 cell.backView.layer.cornerRadius = 15 The result is the following : ![Result](http://nl.tinypic.com/r/6ekfaw/9). – Thomas L Apr 26 '17 at 08:57
  • using auto layout like leading,traling,top and bottom constraint. to 10 – ankit Apr 26 '17 at 08:58
  • This is the result when I try this : [result with margin](http://nl.tinypic.com/r/xm9a8j/9) and [result in simulator](http://nl.tinypic.com/r/32zl1et/9) – Thomas L Apr 26 '17 at 09:13
  • Actually you need to clear all constraints and then reapply . – ankit Apr 26 '17 at 09:17
  • This did not have the result I wanted, I want the border shadow to be on the outside borders of the cell itself, now it puts a shadow on the inside of the cell. – Thomas L Apr 26 '17 at 09:51
  • well you can decrease the constrain to 2 so that it look as cell. itself – ankit Apr 26 '17 at 10:00