Cum sa adaug bordura pentru un section header – UITableVIew

Se da un UITableVIew in Swift 3 in care avem doua sectiuni. Cea de-a doua se numeste ACCOUNT.
Section Title UIViewTable

Cerinta pe care o am este sa adaug un separator intre cele doua sectiuni ale tableview-ului, cam ca-n poza:

Section with separator UITableView

Cum o rezolvam? Definim o extensie CALayer care sa adauge o bordura acolo unde s-a specificat:

extension CALayer {

func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
let border = CALayer()
switch edge {
border.frame = CGRect.init(x: 0, y: 0, width: frame.width, height: thickness)
border.backgroundColor = color.cgColor

Folosim aceasta extensie de layer in delegate-ul tableView(_:willDisplayHeaderView:forSection:) cam asa:

let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.contentView.layer.addBorder(edge: [.top], color:, thickness: 3)

Trebuie retinut faptul ca addBorder este metoda pe layer si nu pe contentView.

Share Button


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.