7

I've found some questions and answers to remove offset of UITableViews in ios7, namely this one here How to fix UITableView separator on iOS 7?

I was wondering if anyone had come across the correct functions to remove inset margins. Something similar to this answer in objective-c

if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [tableView setSeparatorInset:UIEdgeInsetsZero];
}
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
johnnywu
  • 235
  • 1
  • 4
  • 18
  • Possible duplicate of [iOS 8 UITableView separator inset 0 not working](http://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working) – swiftBoy Aug 19 '16 at 06:18

5 Answers5

14

You can just set the property: tableView.separatorInset = UIEdgeInsetsZero

Kamil
  • 196
  • 3
  • 1
    This post is being automatically flagged as low quality because it is so short / only code. Would you mind expanding it by adding some text to explain how it solves the problem? – gung - Reinstate Monica Jul 15 '14 at 14:26
  • 2
    hey this should normally work with the tableview that I have, yet my table view still has the border included. it might be that something is overriding my setting – johnnywu Jul 16 '14 at 17:06
9

Just like the Objective-C example, but converted to swift. I had some trouble myself. This code works in a UITableView if you were doing it in a UITableViewController you would substitute self.tableView for self:

// iOS 7
if(self.respondsToSelector(Selector("setSeparatorInset:"))){
    self.separatorInset = UIEdgeInsetsZero
}

// iOS 8
if(self.respondsToSelector(Selector("setLayoutMargins:"))){
    self.layoutMargins = UIEdgeInsetsZero;
}

And for the cell (iOS 8 only) put the code below in the following function:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)

get the cell, and set the following property:

// iOS 8
if(cell.respondsToSelector(Selector("setLayoutMargins:"))){
    cell.layoutMargins = UIEdgeInsetsZero;
}
Kendrick Taylor
  • 2,218
  • 2
  • 18
  • 21
3

Put the following lines in viewDidLoad()

tableView.layoutMargins = UIEdgeInsetsZero
tableView.separatorInset = UIEdgeInsetsZero

Now look for your cellForRowAtIndexPath method and add this:

cell.layoutMargins = UIEdgeInsetsZero

nowadays ".zero" syntax...

    tableView.layoutMargins = UIEdgeInsets.zero
    tableView.separatorInset = UIEdgeInsets.zero

    cell.layoutMargins = UIEdgeInsets.zero
Fattie
  • 27,874
  • 70
  • 431
  • 719
mrmike
  • 398
  • 2
  • 4
  • 14
1

for Swift 3 just type:

tableView.separatorInset = .zero
Gilad Brunfman
  • 3,452
  • 1
  • 29
  • 29
1

You can do this via the console by modifying the "Default Insets" to be "Custom Insets"

  1. Inside the table view cell click the slider icon
  2. Under Table View Cell go to where Separator says "Default Insets"
  3. Click the dropdown menu and select "Custom Insets"
  4. Choose your left and right number (I have mine set to 0 for edge to edge divider)

Image showing default settings

Imgage showing custom settings