Thomas L
Thomas L

Reputation: 330

Putting box shadow on UITableView cells not showing [Swift 3]

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.

Upvotes: 0

Views: 1245

Answers (1)

ankit
ankit

Reputation: 3647

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

Upvotes: 3

Related Questions