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?
Simple solution in Swift for iOS 8 with a custom
UITableViewCell
In this way you are setting
layoutMargin
andseparatorInset
just one time instead of doing it for eachwillDisplayCell
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 intableView:cellForRowAtIndexPath
.Just another hint: you don't need to set
preservesSuperviewLayoutMargins = false
because default value is alreadyNO
!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!
Additionally, 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 toNO
will allow the cell'slayoutMargin
setting to override whateverlayoutMargin
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:
Swift 4:
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:
Swift 4:
...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
toNO
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.
Swift 4:
(above code from iOS 8 UITableView separator inset 0 not working)
EDIT: Here's a pure Interface Builder approach:
NOTE: iOS 11 changes & simplifies much of this behavior, an update will be forthcoming...
As to what cdstamper suggested instead of the table view, adding below lines in the cell's layoutSubview method works for me.
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:
0
Put like that, it's a pretty simple task to achieve:
Things to note:
preservesSuperviewLayoutMargins
if desired.preservesSuperviewLayoutMargins
, you can configure ancestor views (such as the table) to have0
left margin too, but this seems inherently more error-prone as you don't control that entire hierarchy.0
and leave the others be.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!)Instead of updating
preservesSuperviewLayoutMargins
andlayoutMargins
every time the cell scrolls in (usingwillDisplayCell
), I'd suggest to do it once incellForRowAtIndexPath:
: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 yourUITableviewDelegate
.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: