UITableViewCell: rounded corners and shadow

前端 未结 14 1317
故里飘歌
故里飘歌 2020-12-02 05:05

I\'m changing the width of a UITableViewCell so that the cell is smaller but the user can still scroll along the edges of the tableview.

override func layout         


        
相关标签:
14条回答
  • 2020-12-02 05:24

    It works without additional views!

    override func awakeFromNib() {
        super.awakeFromNib()
    
        layer.masksToBounds = false
        layer.cornerRadius = Constants.cornerRadius
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ...
        let layer = cell.layer
        layer.shadowOffset = CGSize(width: 0, height: 1)
        layer.shadowRadius = 2
        layer.shadowColor = UIColor.lightGray.cgColor
        layer.shadowOpacity = 0.2
        layer.frame = cell.frame
        cell.tagLabel.text = tagItems[indexPath.row].text
    
        return cell
    }
    
    0 讨论(0)
  • 2020-12-02 05:31

    1- Create Custom TableViewCell Class . Paste the following code at class level right where you create IBOutlets. exerciseView is the view just inside ContentView to which you want to round.

    @IBOutlet weak var exerciseView: UIView! {
            didSet {
                self.exerciseView.layer.cornerRadius = 15
                self.exerciseView.layer.masksToBounds = true
            }
        }
    

    didSet is variable observer basically. You can do this in awakeFromNib function as well as:

    self.exerciseView.layer.cornerRadius = 15
    self.exerciseView.layer.masksToBounds = true
    
    0 讨论(0)
提交回复
热议问题