0

I need to change custom UITableview section header corner radius dynamically. Initially section header corner radius is 5 for all corners. After adding cell to section, bottom left and bottom right corners should remove. I changed that in layout subview method of custom UITableViewHeaderFooter class. But its not reflecting, corner radius added for all corners, not changing when adding cell. My code is:

   override func layoutSubviews() {
    super.layoutSubviews()
    textLabel?.textColor = UIColor.black
    contentView.backgroundColor = UIColor.clear
    backgroundView?.backgroundColor = UIColor.white
    if let sections = self.sections, let sectionIndex = self.sectionIndex, let values = sections[sectionIndex].values, values.count > 0, sections[sectionIndex].expanded {
        layer.cornerRadius = 5
        self.setRoundCorners(cornerRadius: 5, corners: [.topLeft, .topRight])
    } else {
        layer.cornerRadius = 5
        layer.mask = nil
    }
}

UIView extension

func setRoundCorners(cornerRadius: CGFloat, corners: UIRectCorner) {

    let path = UIBezierPath(roundedRect: bounds,
                            byRoundingCorners: corners,
                            cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
    let maskLayer = CAShapeLayer()
    maskLayer.path = path.cgPath
    layer.mask = maskLayer
}
Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37
Dharani
  • 1
  • 2

0 Answers0