Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Apply border around tableView Sections
/**
Extension for UITableViewController or UIViewController as you prefer
*/
extension UITableViewController {
func colorSection(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let cornerRadius: CGFloat = 0.0
cell.backgroundColor = UIColor.clear
let layer: CAShapeLayer = CAShapeLayer()
let pathRef: CGMutablePath = CGMutablePath()
//dx leading an trailing margins
let bounds: CGRect = cell.bounds.insetBy(dx: 0, dy: 0)
var addLine: Bool = false
if indexPath.row == 0 && indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
pathRef.__addRoundedRect(transform: nil, rect: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius)
} else if indexPath.row == 0 {
pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.minY),
tangent2End: CGPoint(x: bounds.midX, y: bounds.minY),
radius: cornerRadius)
pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.minY),
tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY),
radius: cornerRadius)
pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
addLine = true
} else if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.minY))
pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.maxY),
tangent2End: CGPoint(x: bounds.midX, y: bounds.maxY),
radius: cornerRadius)
pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.maxY),
tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY),
radius: cornerRadius)
pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
} else {
pathRef.addRect(bounds)
addLine = true
}
layer.path = pathRef
layer.strokeColor = UIColor.lightGray.cgColor
layer.lineWidth = 0.5
layer.fillColor = UIColor.white.cgColor
if addLine == true {
let lineLayer: CALayer = CALayer()
let lineHeight: CGFloat = (1 / UIScreen.main.scale)
lineLayer.frame = CGRect(x: bounds.minX, y: bounds.size.height - lineHeight, width: bounds.size.width, height: lineHeight)
lineLayer.backgroundColor = UIColor.clear.cgColor
layer.addSublayer(lineLayer)
}
let backgroundView: UIView = UIView(frame: bounds)
backgroundView.layer.insertSublayer(layer, at: 0)
backgroundView.backgroundColor = .white
cell.backgroundView = backgroundView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.