How do I adjust the indentation of a TableView title in Swift?

I know how to set the title text and style it, but for the life of me I can't find anything that explains how to adjust the indentation of the text. This is what I tried:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.translatesAutoresizingMaskIntoConstraints = false
    header.textLabel?.leftAnchor.constraint(equalTo: header.leftAnchor, constant: 15).isActive = true
}

      

The above code (and every variation I've tried) results in an error:

Constraints cannot be activated because they do not have a common ancestor.

What makes me say on my computer: "If you header.textLabel

brought me to UILabel

, and header

really is UITableViewHeaderFooterView

, I believe that you should know that he is the ancestor of another!"

+3


source to share


1 answer


You can do this by presetting separatorInset

everything TableView

like this:

let headerInset: CGFloat = 50
tableView.separatorInset = UIEdgeInsets.init(top: 0, left: headerInset, bottom: 0, right: 0)

      



Then, if you want to position the table cells in a ruler with an edge TableView

, you can do so in the method forRowAt indexPath

:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    cell.separatorInset = UIEdgeInsets.zero

}

      

+3


source







All Articles