Change tableView separator insets

17,713

Solution 1

For conversion of the answer (Separator lines for UITableViewCellStyleSubtitle cells not taking the full width) in Swift

enter image description here

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("yourcell") as! UITableViewCell

    if (cell.respondsToSelector("setPreservesSuperviewLayoutMargins:")){
        cell.layoutMargins = UIEdgeInsetsZero
        cell.preservesSuperviewLayoutMargins = false
    }
}

By Checking version number :

let floatVersion = (UIDevice.currentDevice().systemVersion as NSString).floatValue

if (floatVersion > 8.0){
    cell.layoutMargins = UIEdgeInsetsZero
    cell.preservesSuperviewLayoutMargins = false
}

Solution 2

Swift 3:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if (cell.responds(to: #selector(setter: UITableViewCell.separatorInset))) {
        cell.separatorInset = UIEdgeInsets.zero
    }

    if (cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins))) {
        cell.preservesSuperviewLayoutMargins = false
    }

    if (cell.responds(to: #selector(setter: UIView.layoutMargins))) {
        cell.layoutMargins = UIEdgeInsets.zero
    }
}

Solution 3

This is old version and it doesn't work anymore.

cell.layoutMargins = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false

You can use this (for swift, in Objective C you can call the same thing)

cell.separatorInset = UIEdgeInsets.zero

Solution 4

the second link should work.

swift version:

if cell.respondsToSelector(Selector("setSeparatorInset:")) {
    cell.separatorInset = UIEdgeInsetsZero
}

if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
    cell.preservesSuperviewLayoutMargins = false
}

if cell.respondsToSelector(Selector("setLayoutMargins:")) {
    cell.layoutMargins = UIEdgeInsetsZero
}

Solution 5

Add these lines in your cellForRowAtIndePath method:

    if cell.respondsToSelector("setSeparatorInset:") {
        cell.separatorInset = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setLayoutMargins:") {
        cell.layoutMargins = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
        cell.preservesSuperviewLayoutMargins = false
    }
Share:
17,713
Fabio Cenni
Author by

Fabio Cenni

Updated on July 22, 2022

Comments

  • Fabio Cenni
    Fabio Cenni almost 2 years

    I'm incurred in a little problem customizing my cell;

    enter image description here

    as you can see the separator line do not reach the left border of the cell, and a I'd like to do it. I found these:

    Can anyone help me to translate in swift code?

  • Ashish Kakkad
    Ashish Kakkad almost 9 years
    @FabioCelli did you added separator custom insets to 0?
  • Fabio Cenni
    Fabio Cenni almost 9 years
    I had already do it, but did not work; now closing and reopening Xcode works (???).
  • Ashish Kakkad
    Ashish Kakkad almost 9 years
    @FabioCelli That's good. You have to just convert my obj-c answer to swift then it should work well :) Anyways happy to hear that it is working.
  • Loegic
    Loegic almost 9 years
    @iAshish, FYI, Apple doesn't want us to use respondToSelector anymore, you can find information about it in the stateoftheunion video from the WWDC
  • Ashish Kakkad
    Ashish Kakkad almost 9 years
    @Loegic Ok, I we can check by the version number because iOS 7 does not have preservesSuperviewLayoutMargins. Updated the answer.
  • Ashish Kakkad
    Ashish Kakkad almost 9 years
    @Loegic can you give me the link in that they have written that you can not use the respondToSelector ?
  • Loegic
    Loegic almost 9 years
    @iAshish You can watch it here (Swift 2 improvement) developer.apple.com/videos/wwdc/2015/?id=102 and I believe Natashatherobot spoke about it on her blog natashatherobot.com
  • Loegic
    Loegic almost 9 years
    @iAshish I found it, it's a new API called availability checking (#available()), implement it this way : The following code snippet is copied from the WWDC2015 Session 106 What's New in Swift The Old Approach: @ IBOutlet var dropButton: NSButton! override func awakeFromNib() { if dropButton.respondsToSelector("setSpringLoaded:") { dropButton.springLoaded = true } } The Better Approach: @ IBOutlet var dropButton: NSButton! override func awakeFromNib() { if #available(OSX 10.10.3, *) { dropButton.springLoaded = true } }
  • Ashish Kakkad
    Ashish Kakkad almost 9 years
    @Loegic Ok. I will check. Thanks for the links :)