Last active
August 23, 2024 09:39
-
-
Save micheltlutz/de304ce9559605792f30056b5b693fe3 to your computer and use it in GitHub Desktop.
Apply border around tableView Sections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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
this is what I was looking for but instead of using in tableview cell I ended up creating a custom UIView.