Keep UITableView padding on header but no in separator on IOS7

Alessio Crestani picture Alessio Crestani · Jan 9, 2014 · Viewed 20.8k times · Source

with the transition on iOS7 the separator has 15px padding on left. I know that i can remove this padding with separator inset feature on the UITableView setting in the xib file, but i need to keep heading text with padding. How to do it?

default:

enter image description here

with custom on separator inset to 0:

enter image description here

i need to keep separators like figure 2, but the header with "2013" like picture 1.

Answer

Neiman Aleksei picture Neiman Aleksei · Mar 27, 2016

USING SWIFT AND STORYBOARD

In storyboard you can set different separator insets for the TableView (header) and for Cells (separators).

Set the separator inset for cell to 0, by selecting the cell in Storyboard and setting the following:

enter image description here

Then set the separator inset for the TableView (for the header) to 15, by selecting the TableView in Storyboard and setting the following:

enter image description here

OPTIONAL:

You may also need to set the margins on the cells to be zero programmatically to ensure that the separator goes all the way from left to right. Use this code if needed in your table view controller's swift file:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("YOURCELL", forIndexPath: indexPath) as! UITableViewCell

    // THIS IS THE IMPORTANT PART...
    cell.layoutMargins = UIEdgeInsetsZero
    cell.preservesSuperviewLayoutMargins = false

    return cell

}