SWIFT UITableViewCell with corner radius and shado

2019-03-21 06:23发布

I've been trying to create a custom tableview cell with rounded corners and drop shadow, I managed to create the rounded corners but the shadow is showing only on the corners and no where else.

标签: ios swift swift3
3条回答
狗以群分
2楼-- · 2019-03-21 06:35

write below code in cellForRowAt indexPath

 cell.contentView.layer.cornerRadius = 10
 cell.contentView.layer.shadowColor = UIColor.blue.cgColor
 cell.contentView.layer.shadowRadius = 3
 cell.contentView.layer.shadowOpacity = 1.0                  
 cell.contentView.clipsToBounds = true
查看更多
Fickle 薄情
3楼-- · 2019-03-21 06:37

For both the shadow and the rounded corners, you can use this code:

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

     let cell = tableView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
     cell.layer.cornerRadius = 10
     let shadowPath2 = UIBezierPath(rect: cell.bounds)
     cell.layer.masksToBounds = false
     cell.layer.shadowColor = UIColor.black.cgColor
     cell.layer.shadowOffset = CGSize(width: CGFloat(1.0), height: CGFloat(3.0))
     cell.layer.shadowOpacity = 0.5
     cell.layer.shadowPath = shadowPath2.cgPath
     return cell
 }

And you can adjust the values and you'll get the perfect shadow for you!

Hope it helps!

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-03-21 06:43

seems like UITableviewCell doesnt support drop shadows. Adding drop shadow to table view cell, please take a look at this.

查看更多
登录 后发表回答