Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
}
like image 383
johnnywu Avatar asked Jul 15 '14 00:07

johnnywu


4 Answers

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

like image 185
Kamil Avatar answered Dec 07 '22 05:12

Kamil


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;
}
like image 38
Kendrick Taylor Avatar answered Dec 07 '22 06:12

Kendrick Taylor


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
like image 22
mrmike Avatar answered Dec 07 '22 04:12

mrmike


for Swift 3 just type:

tableView.separatorInset = .zero
like image 30
Gilad Brunfman Avatar answered Dec 07 '22 06:12

Gilad Brunfman