How to set the full width of separator in UITableView

Timo Cengiz picture Timo Cengiz · Oct 23, 2014 · Viewed 85.8k times · Source

I have a UITableView where the separators don't have the full width. It ends like 10 pixels before the left side. I was playing around with this code in the viewDidLoad().

self.tableView.layoutMargins = UIEdgeInsetsZero;

Also in the storyboard when you can select custom or default selectors. Now all the cells that are populated don't have the full-width selectors but the cells that are empty have full width.

How can I fix this?

Answer

MB_iOSDeveloper picture MB_iOSDeveloper · Sep 23, 2015

This worked for me on iOS 8.4 - 9.0 devices using Xcode 6.4 and Swift 1.2:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = UITableViewCell()


    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsetsZero
    cell.layoutMargins = UIEdgeInsetsZero

    return cell
}

Swift 5 Update:

cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero