671

I have an app where the UITableView's separator inset is set to custom values - Right 0, Left 0. This works perfectly in iOS 7.x, however in iOS 8.0 I see that the separator inset is set to the default of 15 on the right. Even though in the xib files it set to 0, it still shows up incorrectly.

How do I remove the UITableViewCell separator margins?

Shobhakar Tiwari
  • 7,086
  • 2
  • 30
  • 65
user3570727
  • 9,835
  • 4
  • 15
  • 23
  • Have you tried setting the inset to 0.001 instead of 0? I remember having a similar problem where the edge inset would not react to 0 but did react to 0.001 (or something similar small). – freshking Sep 10 '14 at 16:44
  • I tried that and it did not work either.. – user3570727 Sep 10 '14 at 16:54
  • 4
    Then I would suggest setting the separators color to `[UIColor clearColor]` and drawing your own separators. You are much more flexible that way. – freshking Sep 10 '14 at 17:01
  • 1
    I still can't believe that this is still not one of the `UITableViewCellSeparatorStyle` defaults. Even the accepted answer is a really dirty hack in my opinion. – Enrico Susatyo Dec 18 '14 at 07:09

43 Answers43

1075

iOS 8.0 introduces the layoutMargins property on cells AND table views.

This property isn't available on iOS 7.0 so you need to make sure you check before assigning it!

The easy fix is to subclass your cell and override the layout margins property as suggested by @user3570727. However you will lose any system behavior like inheriting margins from the Safe Area so I do not recommend the below solution:

(ObjectiveC)

-(UIEdgeInsets)layoutMargins { 
     return UIEdgeInsetsZero // override any margins inc. safe area
}

(swift 4.2):

override var layoutMargins: UIEdgeInsets { get { return .zero } set { } }

If you don't want to override the property, or need to set it conditionally, keep reading.


In addition to the layoutMargins property, Apple has added a property to your cell that will prevent it from inheriting your Table View's margin settings. When this property is set, your cells are allowed to configure their own margins independently of the table view. Think of it as an override.

This property is called preservesSuperviewLayoutMargins, and setting it to NO will allow the cell's layoutMargin setting to override whatever layoutMargin is set on your TableView. It both saves time (you don't have to modify the Table View's settings), and is more concise. Please refer to Mike Abdullah's answer for a detailed explanation.

NOTE: what follows is a clean implementation for a cell-level margin setting, as expressed in Mike Abdullah's answer. Setting your cell's preservesSuperviewLayoutMargins=NO will ensure that your Table View does not override the cell settings. If you actually want your entire table view to have consistent margins, please adjust your code accordingly.

Setup your cell margins:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
           [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

Swift 4:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    // Remove seperator inset
    if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        cell.separatorInset = .zero
    }
    // Prevent the cell from inheriting the Table View's margin settings
    if cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {
        cell.preservesSuperviewLayoutMargins = false
    }
    // Explictly set your cell's layout margins
    if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
        cell.layoutMargins = .zero
    }
}

Setting the preservesSuperviewLayoutMargins property on your cell to NO should prevent your table view from overriding your cell margins. In some cases, it seems to not function properly.

If all fails, you may brute-force your Table View margins:

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    // Force your tableview margins (this may be a bad idea)
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
} 

Swift 4:

func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    // Force your tableview margins (this may be a bad idea)
    if tableView.responds(to: #selector(setter: UITableView.separatorInset)) {
        tableView.separatorInset = .zero
    }
    if tableView.responds(to: #selector(setter: UITableView.layoutMargins)) {
        tableView.layoutMargins = .zero
    }
}

...and there you go! This should work on iOS 7 and 8.


EDIT: Mohamed Saleh brought to my attention a possible change in iOS 9. You may need to set the Table View's cellLayoutMarginsFollowReadableWidth to NO if you want to customize insets or margins. Your mileage may vary, this is not documented very well.

This property only exists in iOS 9 so be sure to check before setting.

if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
{
    myTableView.cellLayoutMarginsFollowReadableWidth = NO;
} 

Swift 4:

if myTableView.responds(to: #selector(setter: self.cellLayoutMarginsFollowReadableWidth)) {
    myTableView.cellLayoutMarginsFollowReadableWidth = false
}

(above code from iOS 8 UITableView separator inset 0 not working)

EDIT: Here's a pure Interface Builder approach:

TableViewAttributesInspector TableViewCellSizeInspector

NOTE: iOS 11 changes & simplifies much of this behavior, an update will be forthcoming...

chris stamper
  • 12,726
  • 1
  • 21
  • 46
  • 2
    For tableviews inside a UIPopoverController, I had to do both of these as shown above, instead of just in the willDisplayCell. – Zhang Sep 21 '14 at 06:19
  • 44
    I also found that despite all the code above that I still had to add `cell.preservesSuperviewLayoutMargins = NO` per @bffmike's suggestion or after scrolling a ways the insets would appear. (wtf??) – inorganik Sep 22 '14 at 20:18
  • What if we want to set the margin only for one type of cell in a grouped tableView? – SAHM Oct 06 '14 at 21:57
  • You could try setting it in willDisplayCell and seeing if that actually works (you have a handle on the cell so it should be trivial). If it doesn't, we need to file a bug. Cell specific layout margins should be possible, IMO. – chris stamper Oct 07 '14 at 15:26
  • 2
    This worked for me with the addition of ``cell.preservesSuperviewLayoutMargins`` in my data source method: ``- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath`` – mts Oct 11 '14 at 14:30
  • Shouldn't you also pass the call to `super` in `viewDidLayoutSubviews`? – Mr Rogers Oct 31 '14 at 14:52
  • "Setup your cell margins" step using iOS 8.1 and Swift was enough for me to make it work. – King-Wizard Dec 23 '14 at 16:27
  • For the Swift version, working and tested on iOS 7 and 8, please see my answer below this post. – King-Wizard Dec 24 '14 at 08:56
  • [self.tableView setSeparatorInset:UIEdgeInsetsZero]; will make section title no inset in grouped tableView. – NSDeveloper Jul 18 '15 at 04:57
  • Same answer in swift in case if any one needed { if ( self.tableVIew .respondsToSelector("setSeparatorInset:") ){ self.tableVIew.separatorInset = UIEdgeInsetsZero } if ( self.tableVIew .respondsToSelector("setLayoutMargins:") ) { self.tableVIew.layoutMargins = UIEdgeInsetsZero }} – Droid GEEK Aug 21 '15 at 10:37
  • `tableView.layoutMargins = UIEdgeInsetZero;` trigers layout and dataSource methods being called immediately. So I prefer to add a method for customized cell, like `- (UIEdgeInsets)layoutMargins { return UIEdgeInsetsZero; }` @King-Wizard – DawnSong Sep 01 '15 at 13:30
  • Note: it did not work me on `UITableViewCell` descentants, only on the main class. – gklka Sep 11 '15 at 20:05
  • This worked for me: `UIEdgeInsetsMake(0, CGRectGetWidth(self.bounds)/2.0, 0, CGRectGetWidth(self.bounds)/2.0)`. Taken from [here](http://johnszumski.com/blog/hiding-table-separators-on-a-cell-by-cell-basis) – Martin Berger Oct 26 '15 at 15:42
  • And you can do this in storyboard: Size Inspector (`cmd+alt+5`) -> Layout Margins section. – derpoliuk Nov 03 '15 at 11:46
  • Please add answer `http://stackoverflow.com/a/32860532/1565971` this to your answer also , as iOS9 have different handling and that appears to me on iPad Only – Mohamed Saleh Jan 17 '16 at 12:32
  • This works for me (applied on dynamic prototype cells): - Set the separator custom insets to 0 on the cell. - Set the Layout Margin of the cell to Explicit, and 0 for the left margin. No code involved. – Sergiu Todirascu Mar 08 '16 at 15:48
  • It seems all you should need to do is set the separatorInset, but instead it does seem like you need to set both separatorInset and the layoutMargins as the answer. But what if you want to use the margins for auto-layout of the content of the cell? – David Pettigrew Sep 16 '16 at 17:05
  • Do you happen to have an update for this answer for iOS 11? It no longer works. – SAHM Oct 26 '17 at 15:04
259

Arg!!! After playing around either doing this in your Cell subclass:

- (UIEdgeInsets)layoutMargins
{
    return UIEdgeInsetsZero;
}

or setting the cell.layoutMargins = UIEdgeInsetsZero; fixed it for me.

jordanhill123
  • 3,991
  • 2
  • 29
  • 39
user3570727
  • 9,835
  • 4
  • 15
  • 23
  • 13
    the first solution in this answer worked for me, the 2nd one didn't work. – spybart Sep 10 '14 at 18:31
  • 5
    **Don't** just use cell.layoutMargins = UIEdgeInsetsZero; as it will crash on iOS 7.x. It better to check if the selector exists first before using it. See my solution below. – Ricky Sep 11 '14 at 12:56
  • 3
    He is not setting cell.layoutMargins ... This solution works great when you already have custom cells. – Sjoerd Perfors Sep 16 '14 at 10:50
  • 11
    This will work fine in iOS 7, as the method won't get called! You also need to set the table view's layoutMargins, though, or it won't work. – chris stamper Sep 16 '14 at 20:20
  • 2
    overriding the accessor worked for me. but all the other solutions did not. even configuring the `separatorInset` in IB did not work. there's also a problem with table view background colors. this is really sick stuff. – glasz Sep 21 '14 at 17:31
  • 1
    here is my observation (on iOS8): UITableViewController want's that solution. For TableViews inside UIViewController the accepted answer does the trick. – Jakob Nov 08 '14 at 14:32
  • 2
    This deservers more up votes. It avoids all that OS specific code. – GoodSp33d Nov 15 '14 at 17:50
  • Overriding layoutMargins method is the most clean solution when using custom cells. – tsafrir Nov 19 '14 at 08:05
  • This *only* works if you've subclassed the cell, however. It also doesn't take into account the similar property on the Table View. – chris stamper Dec 08 '14 at 15:51
  • Overriding layoutMargins method worked (iOS 8.3), setting property was not. – esbenr May 09 '15 at 20:35
  • `tableView.layoutMargins = UIEdgeInsetZero; ` **trigers layout and dataSource methods being called immediately**. So your solution is preferred. – DawnSong Sep 01 '15 at 13:40
  • Overriding layoutMargins did not work for me on Xcode 8.3.2 iOS 10.2.1 but setting self.separatorInset = UIEdgeInsetsZero in my init method worked. – Leon Jul 17 '17 at 10:38
179

Let's take a moment to understand the problem before blindly charging in to attempt to fix it.

A quick poke around in the debugger will tell you that separator lines are subviews of UITableViewCell. It seems that the cell itself takes a fair amount of responsibility for the layout of these lines.

iOS 8 introduces the concept of layout margins. By default, a view's layout margins are 8pt on all sides, and they're inherited from ancestor views.

As best we can tell, when laying out out its separator line, UITableViewCell chooses to respect the left-hand layout margin, using it to constrain the left inset.

Putting all that together, to achieve the desired inset of truly zero, we need to:

  • Set the left layout margin to 0
  • Stop any inherited margins overriding that

Put like that, it's a pretty simple task to achieve:

cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;

Things to note:

  • This code only needs to be run once per cell (you're just configuring the cell's properties after all), and there's nothing special about when you choose to execute it. Do what seems cleanest to you.
  • Sadly neither property is available to configure in Interface Builder, but you can specify a user-defined runtime attribute for preservesSuperviewLayoutMargins if desired.
  • Clearly, if your app targets earlier OS releases too, you'll need to avoid executing the above code until running on iOS 8 and above.
  • Rather than setting preservesSuperviewLayoutMargins, you can configure ancestor views (such as the table) to have 0 left margin too, but this seems inherently more error-prone as you don't control that entire hierarchy.
  • It would probably be slightly cleaner to set only the left margin to 0 and leave the others be.
  • If you want to have a 0 inset on the "extra" separators that UITableView draws at the bottom of plain style tables, I'm guessing that will require specifying the same settings at the table level too (haven't tried this one!)
Mike Abdullah
  • 14,782
  • 2
  • 47
  • 73
  • 3
    Thank you! This really is the more concise answer. Much better than overriding two methods and brute forcing it. – LunaCodeGirl Nov 14 '14 at 04:46
  • 16
    In iOS 8, for me, this wouldn't work without `[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];` – davetw12 Dec 06 '14 at 01:47
  • 1
    You have my vote, thanks for the clear explanation. I'll just mention that the TableView itself also has layoutMargins that may need to be set in addition to the separatorInset, in some cases. – chris stamper Dec 06 '14 at 18:36
  • @davetw12 and @cdstamper Can you provide any evidence of this? All my explorations so far say that setting both `layoutMargins` and `preservesSuperviewLayoutMargins` on the cell is all that is required. Anything else feels like trusting in voodoo. – Mike Abdullah Dec 07 '14 at 20:45
  • No. I agree, it seems that setting preservesSuperviewLayoutMargins would prevent the cell from inheriting the table view settings; however, I have seen that fail at least once. – chris stamper Dec 08 '14 at 15:56
  • OK. If you can provide some sort of test case, I'd be delighted to dig into it. – Mike Abdullah Dec 08 '14 at 17:18
  • 3
    No, it's not a form of voodoo @MikeAbdullah. In order for you to avoid the default `separatorInset` of a `UITableViewCell` in iOS 8, you have to set the set the `UIEdgeInsets` to zero on both the `UITableView` and the `UITableViewCell`. If you don't do both, you'll still have a left inset that is greater than zero. I have confirmed this multiple times. – davetw12 Dec 08 '14 at 23:33
  • @davetw12 Not according to my findings. Setting `preservesSuperviewLayoutMargins` to `NO` overrides that inherited margin – Mike Abdullah Dec 09 '14 at 09:04
  • Testing with iOS 8.1 in the simulator, setting `layoutMargins` and `preservesSuperviewLayoutMargins` on the cell alone is sufficient, even for the "extra" separators that the tableview draws on its own. More interestingly, the "extra" separators ignore layout margins even if I don't change those things; if I merely set `separatorInset` to `UIEdgeInsetsZero` then the cell separators are inset but the "extra" ones aren't. – Lily Ballard Jan 08 '15 at 19:27
  • Ah, I hadn't considered "extra" separators, as I've been focusing on grouped style tables. Interesting to know, thanks, Kevin! – Mike Abdullah Jan 09 '15 at 20:30
  • `preservesSuperviewLayoutMargins` is `NO` by default. – esh Mar 24 '15 at 08:00
  • @BlackFlam3 it isn't in my experience. Are you talking about `UIView` generally, or a specific kind of view? – Mike Abdullah Mar 25 '15 at 09:00
  • @davetw12 i will have to agree with Mike. I have tried every possible solution and it will not work. After spending one hour on the above simple and stupid problem, I finally created my own separator view inside the cell, which works. – zirinisp Jun 10 '16 at 09:34
59

I believe this is the same question that I asked here: Remove SeparatorInset on iOS 8 UITableView for XCode 6 iPhone Simulator

In iOS 8, there is one new property for all the objects inherit from UIView. So, the solution to set the SeparatorInset in iOS 7.x will not be able to remove the white space you see on the UITableView in iOS 8.

The new property is called "layoutMargins".

@property(nonatomic) UIEdgeInsets layoutMargins
Description   The default spacing to use when laying out content in the view.
Availability  iOS (8.0 and later)
Declared In   UIView.h
Reference UIView Class Reference

iOS 8 UITableView setSeparatorInset:UIEdgeInsetsZero setLayoutMargins:UIEdgeInsetsZero

The solution:-

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

   if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
   }
}

If you set cell.layoutMargins = UIEdgeInsetsZero; without checking if the layoutMargins exists, the app will crash on iOS 7.x. So, the best way would be checking if the layoutMargins exists first before setLayoutMargins:UIEdgeInsetsZero.

Community
  • 1
  • 1
Ricky
  • 10,435
  • 6
  • 34
  • 48
  • How can this approach be used if the deployment target is set to iOS 7 ? – Petar Dec 02 '14 at 17:04
  • I put the code for tableView in viewDidLoad and are working fine. I think its not necessary to call them every time in willDisplayCell(). – Abdullah Umer Jan 19 '15 at 12:54
46

You can use UIAppearance once, at your application startup (before UI is loaded), to set it as default global settings:

// iOS 7:
[[UITableView appearance] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[[UITableView appearance] setSeparatorInset:UIEdgeInsetsZero];

[[UITableViewCell appearance] setSeparatorInset:UIEdgeInsetsZero];

// iOS 8:
if ([UITableView instancesRespondToSelector:@selector(setLayoutMargins:)]) {

    [[UITableView appearance] setLayoutMargins:UIEdgeInsetsZero];
    [[UITableViewCell appearance] setLayoutMargins:UIEdgeInsetsZero];
    [[UITableViewCell appearance] setPreservesSuperviewLayoutMargins:NO];

}

This way, you keep your UIViewController's code clean and can always override it if you want.

Lukasz
  • 19,014
  • 15
  • 77
  • 136
41

iOS introduces the layoutMargins property on cells AND table views.

This property isn't available in iOS 7.0 so you need to make sure you check before assigning it!

However, Apple has added a property called preservesSuperviewLayoutMargins to your cell that will prevent it from inheriting your Table View's margin settings. This way, your cells can configure their own margins independently of the table view. Think of it as an override.

This property is called preservesSuperviewLayoutMargins, and setting it to NO can allow you to override your Table View's layoutMargin settings with your own cell's layoutMargin setting. It both saves time (you don't have to modify the Table View's settings), and is more concise. Please refer to Mike Abdullah's answer for a detailed explanation.

NOTE: this is the proper, less messy implementation, as expressed in Mike Abdullah's answer; setting your cell's preservesSuperviewLayoutMargins=NO will ensure that your Table View does not override the cell settings.

First step - Setup your cell margins:

/*
    Tells the delegate that the table view is about to draw a cell for a particular row.
*/
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
    forRowAtIndexPath indexPath: NSIndexPath)
{
    // Remove separator inset
    if cell.respondsToSelector("setSeparatorInset:") {
        cell.separatorInset = UIEdgeInsetsZero
    }

    // Prevent the cell from inheriting the Table View's margin settings
    if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
        cell.preservesSuperviewLayoutMargins = false
    }

    // Explictly set your cell's layout margins
    if cell.respondsToSelector("setLayoutMargins:") {
        cell.layoutMargins = UIEdgeInsetsZero
    }
}

Setting the preservesSuperviewLayoutMargins property on your cell to NO should prevent your table view from overriding your cell margins. In some cases, it seems not to function properly.

Second step - Only if all fails, you may brute-force your Table View margins:

/*
    Called to notify the view controller that its view has just laid out its subviews.
*/
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // Force your tableview margins (this may be a bad idea)
    if self.tableView.respondsToSelector("setSeparatorInset:") {
        self.tableView.separatorInset = UIEdgeInsetsZero
    }

    if self.tableView.respondsToSelector("setLayoutMargins:") {
        self.tableView.layoutMargins = UIEdgeInsetsZero
    }
}

...and there you go! This should work on iOS 8 as well as iOS 7.

Note: tested using iOS 8.1 and 7.1, in my case I only needed to use the first step of this explanation.

The Second Step is only required if you have unpopulated cell beneath the rendered cells, ie. if the table is larger than the number of rows in the table model. Not doing the second step would result in different separator offsets.

Eric Aya
  • 68,765
  • 33
  • 165
  • 232
King-Wizard
  • 14,974
  • 4
  • 79
  • 74
  • 1
    You don't need to check for selectors in the `willDisplayCell` method as the method itself is already iOS 8+ and will never be called from older versions. – Rivera Jun 15 '15 at 15:14
38

In Swift it's slightly more annoying because layoutMargins is a property, so you have to override the getter and setter.

override var layoutMargins: UIEdgeInsets {
  get { return UIEdgeInsetsZero }
  set(newVal) {}
}

This will effectively make layoutMargins readonly, which in my case is fine.

swilliams
  • 44,959
  • 24
  • 94
  • 129
  • Only this solution did work for me. And I did not need add additional code of lines in the methods. I did use custom cells. – Ramis Oct 01 '14 at 14:02
  • 2
    For static cell, except from overriding layoutMargins, you still need to change cell's separator style to "Custom Insets" and set left value to "0" in storyboard. – hufeng03 Nov 03 '14 at 21:50
23

For iOS 9 you need to add:

if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
{
    myTableView.cellLayoutMarginsFollowReadableWidth = NO;
} 

For more details please refer to question.

Community
  • 1
  • 1
Julian
  • 8,989
  • 4
  • 45
  • 63
20

Swift 2.0 Extension

I just wanted to share an extension I made to remove the margins from the tableview cell separators.

extension UITableViewCell {
    func removeMargins() {

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

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

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

Used in context:

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell

    cell.removeMargins()
    return cell
Dan Beaulieu
  • 17,926
  • 15
  • 92
  • 126
15

Swift:

override func viewDidLoad() {
    super.viewDidLoad()

    if self.tableView.respondsToSelector("setSeparatorInset:") {
        self.tableView.separatorInset = UIEdgeInsetsZero
    }
    if self.tableView.respondsToSelector("setLayoutMargins:") {
        self.tableView.layoutMargins = UIEdgeInsetsZero
    }

    self.tableView.layoutIfNeeded()            // <--- this do the magic
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     ...

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

    return cell
}
Alexander Volkov
  • 6,770
  • 1
  • 41
  • 37
11

I made it work by doing this:

tableView.separatorInset = UIEdgeInsetsZero;
tableView.layoutMargins = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
ideawu
  • 2,081
  • 1
  • 21
  • 27
9

As to what cdstamper suggested instead of the table view, adding below lines in the cell's layoutSubview method works for me.

- (void)layoutSubviews 
{
    [super layoutSubviews];

    if ([self respondsToSelector:@selector(setSeparatorInset:)])
                [self setSeparatorInset:UIEdgeInsetsZero];

        if ([self respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
        {
            [self setPreservesSuperviewLayoutMargins:NO];;
        }

        if ([self respondsToSelector:@selector(setLayoutMargins:)]) 
        {
            [self setLayoutMargins:UIEdgeInsetsZero];
        }
}
P.J.Radadiya
  • 1,372
  • 10
  • 16
Vijay V S
  • 129
  • 5
9

After much investigation...

Here's the only way to fully control this stuff (that I could find)

To fully control both separator insets and layout margins on each cell. Do this in the willDisplayCell method on your UITableviewDelegate.

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.layoutMargins = UIEdgeInsetsZero
    cell.contentView.layoutMargins = UIEdgeInsetsMake(0, 10, 0, 10)
    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
}

The cell object controls the separator, and the contentView controls everything else. If your separator inset spaces are showing up in an unexpected color this should solve it:

cell.backgroundColor = cell.contentView.backgroundColor
Community
  • 1
  • 1
Matjan
  • 3,260
  • 1
  • 30
  • 30
9

Swift 3.0 example:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    // removing seperator inset
    if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        cell.separatorInset = .zero
    }
    // prevent the cell from inheriting the tableView's margin settings
    if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
        cell.preservesSuperviewLayoutMargins = false
    }
    // explicitly setting cell's layout margins
    if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
        cell.layoutMargins = .zero
    }
}
mkz
  • 2,122
  • 1
  • 24
  • 41
8

Simple solution in Swift for iOS 8 with a custom UITableViewCell

override func awakeFromNib() {
    super.awakeFromNib()

    self.layoutMargins = UIEdgeInsetsZero
    self.separatorInset = UIEdgeInsetsZero
}

In this way you are setting layoutMargin and separatorInset just one time instead of doing it for each willDisplayCell as most of the above answers suggest.

If you are using a custom UITableViewCell this is the correct place to do it. Otherwise you should do it in tableView:cellForRowAtIndexPath.

Just another hint: you don't need to set preservesSuperviewLayoutMargins = false because default value is already NO!

Francesco Vadicamo
  • 5,382
  • 33
  • 29
8

For me the simple line did the job

cell.layoutMargins = UIEdgeInsetsZero
apinho
  • 2,045
  • 3
  • 20
  • 37
7

Just add below code can solve this program.

Good luck to you!

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

       if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
           [cell setSeparatorInset:UIEdgeInsetsZero];
       }

       if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
           [cell setLayoutMargins:UIEdgeInsetsZero];
       }

}
JimmyChang
  • 91
  • 1
  • 4
5

This is the code that's working for me, in Swift:

override func viewDidLoad() 
{
    super.viewDidLoad()
    ...
    if tableView.respondsToSelector("setSeparatorInset:") {
        tableView.separatorInset = UIEdgeInsetsZero
    }
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath)
{
    if cell.respondsToSelector("setSeparatorInset:") {
        cell.separatorInset.left = CGFloat(0.0)
    }
    if tableView.respondsToSelector("setLayoutMargins:") {
        tableView.layoutMargins = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setLayoutMargins:") {
        cell.layoutMargins.left = CGFloat(0.0)
    }
}

This seems the cleanest to me (for now), as all the cell/tableView edge/margin adjustments are done in the tableView:willDisplayCell:forRowAtIndexPath: method, without cramming unneccessary code into tableView:cellForRowAtIndexPath:.

Btw, I'm only setting the cell's left separatorInset/layoutMargins, because in this case I don't want to screw up my constraints that I have set up in my cell.

Code updated to Swift 2.2 :

 override func viewDidLoad() {
   super.viewDidLoad()       

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

 override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {
        if cell.respondsToSelector(Selector("setSeparatorInset:")) {
            cell.separatorInset.left = CGFloat(0.0)
        }
        if tableView.respondsToSelector(Selector("setLayoutMargins:")) {
            tableView.layoutMargins = UIEdgeInsetsZero
        }
        if cell.respondsToSelector(Selector("setLayoutMargins:")) {
            cell.layoutMargins.left = CGFloat(0.0)
        }
    }
Jayprakash Dubey
  • 32,447
  • 16
  • 161
  • 169
Ivan
  • 469
  • 4
  • 11
  • What exactly is not working for you? Just tested this code out on the iPhone 5 simulator, with the iOS 8.1 SDK installed, and everything is as it was when I originally posted this answer. Code was compiled & run with Xcode 6.1.1. – Ivan Jan 01 '15 at 16:34
5

Lukasz answer in Swift:

    // iOS 7:
    UITableView.appearance().separatorStyle = .SingleLine
    UITableView.appearance().separatorInset = UIEdgeInsetsZero
    UITableViewCell.appearance().separatorInset = UIEdgeInsetsZero

    // iOS 8:
    if UITableView.instancesRespondToSelector("setLayoutMargins:") {
        UITableView.appearance().layoutMargins = UIEdgeInsetsZero
        UITableViewCell.appearance().layoutMargins = UIEdgeInsetsZero
        UITableViewCell.appearance().preservesSuperviewLayoutMargins = false
    }
Naka
  • 982
  • 12
  • 14
4

Most answers are showing separator insets and layout margins being set over a variety of methods (i.e., viewDidLayoutSubviews, willDisplayCell, etc) for cells and tableviews, but I've found that just putting these in cellForRowAtIndexPath works great. Seems like the cleanest way.

// kill insets for iOS 8
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8) {
    cell.preservesSuperviewLayoutMargins = NO;
    [cell setLayoutMargins:UIEdgeInsetsZero];
}
// iOS 7 and later
if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    [cell setSeparatorInset:UIEdgeInsetsZero];
inorganik
  • 21,174
  • 17
  • 78
  • 101
4

Use below code snippet avoid unwanted padding issue for UITableView in IOS 8 & 7.

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
    {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
Anooj VM
  • 2,485
  • 19
  • 19
4

Instead of updating preservesSuperviewLayoutMargins and layoutMargins every time the cell scrolls in (using willDisplayCell), I'd suggest to do it once in cellForRowAtIndexPath::

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)

    cell.preservesSuperviewLayoutMargins = false
    cell.layoutMargins = UIEdgeInsetsZero

    return cell

}
Rudolf Adamkovič
  • 28,645
  • 11
  • 96
  • 111
4

In iOS8:

Adding this to my UITableViewCell Subclass:

- (UIEdgeInsets)layoutMargins {
    return UIEdgeInsetsZero;
}

and this to "tableView:cellForRowAtIndexPath" or "tableView:willDisplayCell":

[editCell setSeparatorInset:UIEdgeInsetsZero];

WORKED for me.

Saru
  • 833
  • 1
  • 14
  • 22
3

Here's an easy way to globally remove the inset.

In UITableViewCell+Extensions.swift:

import UIKit

extension UITableViewCell {

  override public var layoutMargins: UIEdgeInsets {
    get { return UIEdgeInsetsZero }
    set { }
  }

}

In AppDelegate application:didFinishLaunchingWithOptions::

  UITableViewCell.appearance().separatorInset = UIEdgeInsetsZero

You might think to a) also just override separatorInset in the extension, or b) set the appearance proxy for layoutMargins, instead. Neither will work. Even though separatorInset is indicated to be a property, attempting to override it as a property (or method) generates compiler errors. And setting the appearance proxy for UITableViewCell's layoutMargins (or, for that matter, also setting the appearance proxies for UITableView's layoutMargins and separatorInset) has no effect.

Scott Gardner
  • 7,855
  • 1
  • 39
  • 34
2
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        // ... Get the cell
        cell.separatorInset = UIEdgeInsetsMake(0.f, 20.f, 0.f, [UIScreen mainScreen].bounds.size.width - 20);
        // others
        return cell;
}

For any specific cell you want to hide the separator.

xmurobi
  • 409
  • 1
  • 7
  • 11
2

After having seen the answers at floor 3, I tried to figure out what the relationship of setting up the separator between TableView & TableViewCell and did some test. Here are my conclusions:

  1. we can consider that setting the cell's separator to zero has to move the separator in two steps: first step is to set cell's separatorinset to zero. second step is to set cell's marginlayout to zero.

  2. set the TableView's separatorinset and marginlayout can affect the Cell's separatorinset. However, from the test, I find that the TableView's separatorinset seem to be useless, TableView's marginlayout can actually affect cell's marginlayout.

  3. set Cell's PreservesSuperviewLayoutMargins = false, can cut off TableView's marginlayout effect on Cells.

  4. one of the solutions:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = UITableViewCell()
    
        cell.preservesSuperviewLayoutMargins = false
        cell.separatorInset = UIEdgeInsetsZero
        cell.layoutMargins = UIEdgeInsetsZero
    
        return cell
    }
    
Xin Chen
  • 81
  • 1
  • 2
2

This is my solution. This applies to the custom cell subclass, just add them both to the subclass.

  1. - (UIEdgeInsets)layoutMargins {    
        return UIEdgeInsetsMake(0, 10, 0, 10);
    }
    

2.

self.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);

And it is convenient that you can customize the position of the separator without asking your designer to draw one for you..........

Brad The App Guy
  • 16,204
  • 2
  • 39
  • 59
Guoyu
  • 21
  • 2
1

In a more compact way than the most voted answer...

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([cell respondsToSelector:@selector(setSeparatorInset:)] && [cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)] && [cell respondsToSelector:@selector(setLayoutMargins:)]) {
         [cell setSeparatorInset:UIEdgeInsetsZero];
         [cell setPreservesSuperviewLayoutMargins:NO];
         [cell setLayoutMargins:UIEdgeInsetsZero];
    }

}

subharb
  • 3,032
  • 8
  • 34
  • 66
1

Adding this snippet, simple elegant in Swift works for me in iOS8 :)

    // tableview single line
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.preservesSuperviewLayoutMargins = false
    cell.layoutMargins = UIEdgeInsetsZero
}
ddnl
  • 457
  • 1
  • 6
  • 20
1

This worked perfectly for me in iOS 8 and iOS 9.

For OBJ-C

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  { 
        if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
        {
            [tableView setSeparatorInset:UIEdgeInsetsZero];
        }

        if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
        {
            [tableView setLayoutMargins:UIEdgeInsetsZero];
        }

        if ([cell respondsToSelector:@selector(setLayoutMargins:)])
        {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
         return cell;
    }
jithin
  • 429
  • 7
  • 20
0

let do as my code:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        cell.preservesSuperviewLayoutMargins = NO;
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
Bkillnest
  • 702
  • 8
  • 16
0

I went through all these wonderful answers and realized most of them do not work with iOS 8, or do work but the separator changes size during animation causing unwanted flashing. This is what I ended up doing in my app delegate before creating the window:

[[UITableView appearance] setSeparatorInset:UIEdgeInsetsZero];
[[UITableViewCell appearance] setSeparatorInset:UIEdgeInsetsZero];

if ([UITableView instancesRespondToSelector:@selector(setLayoutMargins:)]) {
    [[UITableView appearance] setLayoutMargins:UIEdgeInsetsZero];
    [[UITableViewCell appearance] setLayoutMargins:UIEdgeInsetsZero];
    [[UITableViewCell appearance] setPreservesSuperviewLayoutMargins:NO];
}

And this is what I added to my UITableViewController:

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

I didn't need to add anything else. Thanks to everyone who provided the crucial bits.

spstanley
  • 940
  • 7
  • 14
0

I didn't have any real luck with any of the solutions above. I'm using NIB files for my tables cells. I "fixed" this by adding a label with a height of 1. I changed the background of the label to black, pinned the label to the bottom of the nib, and then pinned the bottom of the rest of my contents to the added label. Now I have a black border running along the bottom of my cells.

To me, this feels like more of a hack, but it does work.

My only other choice was to just eliminate the border completely. I'm still deciding whether I'll just go with that.

Scott
  • 1,170
  • 2
  • 8
  • 15
0

iOS 8 and later. Swift 2.0 and later:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.separatorInset = UIEdgeInsetsZero
    if #available(iOS 8.0, *) {
        cell.layoutMargins = UIEdgeInsetsZero
    } else {
        // Fallback on earlier versions
    }
}
Zia
  • 14,163
  • 7
  • 37
  • 55
0

following answer from @cdstamper, a better place is layoutSubviews of UITableViewCell, in your cell file(I set 1% spacing, you can set to zero), so need only to set code here to handle all situations(rotate and other):

 -(void)layoutSubviews
    {
        [super layoutSubviews];
        if ([self respondsToSelector:@selector(setSeparatorInset:)]) {
            [self setSeparatorInset:UIEdgeInsetsMake(0,self.bounds.size.width*0.01,0,self.bounds.size.width*0.01)];
        }
        if ([self respondsToSelector:@selector(setLayoutMargins:)]) {
            [self setLayoutMargins:UIEdgeInsetsMake(0,self.bounds.size.width*0.01,0,self.bounds.size.width*0.01)];
        }
    }
BollMose
  • 1,844
  • 2
  • 21
  • 33
0

My solution is adding an UIView as a container for the cell subviews. Then set this UIView constraints (top,bottom,trailing,leading) to 0 points. And all the unnecessary mass go away.the image showing the logic

0

enter image description here

use storyboard to Change Separator inset Custom left 0 and right 0, if you want to hide separator to None see above image

Phani Sai
  • 1,135
  • 15
  • 29
0

In Swift you can use this

    cell.selectionStyle = UITableViewCellSelectionStyle.None
    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsetsZero
    cell.layoutMargins = UIEdgeInsetsZero
Anny
  • 449
  • 5
  • 14
0

simply put this two lines in cellForRowAtIndexPath method

  • if you want to all separator lines are start from zero [cell setSeparatorInset:UIEdgeInsetsZero]; [cell setLayoutMargins:UIEdgeInsetsZero];

if you want to Specific separator line are start from zero suppose here is last line is start from zero

if (indexPath.row == array.count-1) 
{
   [cell setSeparatorInset:UIEdgeInsetsZero];
   [cell setLayoutMargins:UIEdgeInsetsZero];
}
else
   tblView.separatorInset=UIEdgeInsetsMake(0, 10, 0, 0);
Shabnam
  • 1
  • 1
0

For me none has worked except this workaround (Swift 3.0):

extension UIColor {
    static func colorWith(hex:String, alpha: CGFloat) -> UIColor {
        var cString = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

        if cString.hasPrefix("#") {
            cString.remove(at: cString.startIndex)
        }

        if cString.characters.count != 6 {
            return UIColor.gray
        }

        var rgbValue:UInt32 = 0
        Scanner(string: cString).scanHexInt32(&rgbValue)

        return UIColor(red:   CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
                       green: CGFloat((rgbValue & 0x00FF00) >> 8)  / 255.0,
                       blue:  CGFloat( rgbValue & 0x0000FF)        / 255.0,
                       alpha: alpha)
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdendifier, for: indexPath)

    cell.backgroundColor = UIColor.colorWith(hex: "c8c7cc", alpha: 1) // same color of line separator

    return cell
}
Daniele Ceglia
  • 723
  • 7
  • 9
0

Just override the "cellForRowAtIndexPath" and set "cell.preservesSuperviewLayoutMargins = false" and "cell.separatorInset = UIEdgeInsets.zero" and "cell.layoutMargins = UIEdgeInsets.zero"

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell: LayoutTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "LayoutCell") as? LayoutTableViewCell



    let currentLayout = OrderLayouts()[indexPath.row]

    cell.NameLabel?.text = currentLayout.name
    cell.DescrLabel?.text = currentLayout.descr

    if(GlobalVariables.debug){

        cell.DescrLabel?.text = "\(currentLayout.id) \n \(currentLayout.descr)"

    }

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


    return cell

}
MDalprato
  • 1
  • 2
-1

XCode 7.1 iOS 7, 8, 9:

just put these two lines in your TabelViewCell:

    self.layoutMargins = UIEdgeInsetsZero;
    self.preservesSuperviewLayoutMargins = false;

That worked for me

Te Be
  • 1
  • 1
-1

With Swift 2.2

create UITableViewCell extension

import UIKit

extension UITableViewCell {
    func removeMargins() {

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

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

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

Now you can use in your cellForRowAtIndex

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
            cell.removeMargins()//To remove seprator inset 
}
swiftBoy
  • 33,793
  • 26
  • 129
  • 124