UITableViewCell: rounded corners and shadow

前端 未结 14 1399
故里飘歌
故里飘歌 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:18

    Regarding answer R Moyer, the solution is excellent, but the bounds are not always installed after the cellForRowAt method, so as a possible refinement of his solution, it is to transfer the call of the setupShadow() method to the LayoutSubview() for example:

    class ShadowView: UIView {
    
        var setupShadowDone: Bool = false
        
        public func setupShadow() {
            if setupShadowDone { return }
            print("Setup shadow!")
            self.layer.cornerRadius = 8
            self.layer.shadowOffset = CGSize(width: 0, height: 3)
            self.layer.shadowRadius = 3
            self.layer.shadowOpacity = 0.3
            self.layer.shadowColor = UIColor.black.cgColor
            self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, 
    byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 8, height: 
    8)).cgPath
            self.layer.shouldRasterize = true
            self.layer.rasterizationScale = UIScreen.main.scale
        
            setupShadowDone = true
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
            print("Layout subviews!")
            setupShadow()
        }
    }
    

    screenshot

提交回复
热议问题