Search code examples
swiftuitableviewuikituibezierpath

how can i achive this UI on my custom table view cell


i want to achive this custom shape on my tableviewcell how could i do it?

UITableViewCell


Solution

  • Use UIBezierPath with arc.

    Add this class to your view.

    class CellShapeView: UIView {
        
        var fillColor: UIColor = .yellow
        
        private var path: UIBezierPath = UIBezierPath()
        
        override func awakeFromNib() {
            super.awakeFromNib()
            self.backgroundColor = UIColor.clear
        }
        
        override func draw(_ rect: CGRect) {
            super.draw(rect)
            
            //For Round corners
            UIBezierPath(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 10, height: 10)).addClip()
            
            path.move(to: CGPoint(x: 0, y: self.frame.size.height))
            path.addLine(to: CGPoint(x: 0, y: 0))
            path.addLine(to: CGPoint(x: self.frame.size.width, y: 0))
            path.addLine(to: CGPoint(x: self.frame.size.width, y: self.frame.size.height))
            
            path.move(to: CGPoint(x: self.frame.size.width, y: self.frame.size.height))
            
            //Right Side circle arc
            path.addArc(withCenter: CGPoint(x: self.frame.size.width, y: self.frame.size.height/2),
                        radius: 10,
                        startAngle: CGFloat((270 * Double.pi) / 180),
                        endAngle: CGFloat((90 * Double.pi) / 180),
                        clockwise: false)
            
            
            path.close()
            fillColor.setFill()
            path.fill()
            
        }
    }
    

    enter image description here