johnnywu
johnnywu

Reputation: 245

how to remove UITableView offset on the left in Swift

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];
}

Upvotes: 7

Views: 12719

Answers (5)

Linger Software
Linger Software

Reputation: 11

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

Upvotes: 1

Gilad Brunfman
Gilad Brunfman

Reputation: 3502

for Swift 3 just type:

tableView.separatorInset = .zero

Upvotes: 1

mrmike
mrmike

Reputation: 438

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

Upvotes: 3

Kendrick Taylor
Kendrick Taylor

Reputation: 2227

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;
}

Upvotes: 9

Kamil
Kamil

Reputation: 196

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

Upvotes: 14

Related Questions