4

I created this section header title and unfortunately I don't know how to add some padding on the right.

screenshot

Here's my code:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let title: UILabel = UILabel()
    title.text = "Days"
    title.backgroundColor = UIColor.lightGrayColor()
    title.textAlignment = NSTextAlignment.Right
    return title
}
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Kevin Hartwig
  • 41
  • 1
  • 1
  • 3
  • http://stackoverflow.com/questions/21017019/keep-uitableview-padding-on-header-but-no-in-separator-on-ios7.. This seems to be answering your questions. Also how about adding 2 spaces at the end of text? title.text = "Days " – shrutim Apr 03 '16 at 20:23
  • Why not try creating a `UIView` and add your `UILabel` as a subview of that view and use auto layout constraints on the label to add some space on the side of the label? – Wes Apr 04 '16 at 01:25
  • http://stackoverflow.com/questions/9502235/how-to-add-padding-left-on-a-uilabel-created-programmatically can help – Prashant Tukadiya Apr 04 '16 at 05:06

2 Answers2

10

You can do something like this:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
    let label = UILabel(frame: CGRect(x: 15, y: 5, width: tableView.frame.width, height: 20))
    label.text = "Days"
    label.textAlignment = .right
    view.addSubview(label)
    return view
}
Linus
  • 423
  • 5
  • 12
Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54
4

I found that adjusting it in the storyboard does the trick. Click on the tableView and set the Separator Inset on Left and Right edges. Make sure to click on the tableView and not the cell.

Maria
  • 4,471
  • 1
  • 25
  • 26