iOS 8 UITableView separator inset 0 not working
Solution 1
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:
NOTE: iOS 11 changes & simplifies much of this behavior, an update will be forthcoming...
Solution 2
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.
Solution 3
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 have0
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!)
Solution 4
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
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
.
Solution 5
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.
user3570727
Updated on July 08, 2022Comments
-
user3570727 almost 2 years
I have an app where the
UITableView
's separator inset is set to custom values - Right0
, Left0
. This works perfectly iniOS 7.x
, however iniOS 8.0
I see that the separator inset is set to the default of15
on the right. Even though in the xib files it set to0
, it still shows up incorrectly.How do I remove the
UITableViewCell
separator margins? -
spybart over 9 yearsthe first solution in this answer worked for me, the 2nd one didn't work.
-
Ricky over 9 yearsDon'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.
-
Sjoerd Perfors over 9 yearsHe is not setting cell.layoutMargins ... This solution works great when you already have custom cells.
-
chris stamper over 9 yearsThis 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.
-
Zhang over 9 yearsFor tableviews inside a UIPopoverController, I had to do both of these as shown above, instead of just in the willDisplayCell.
-
glasz over 9 yearsoverriding 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. -
inorganik over 9 yearsI 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??) -
Ramis over 9 yearsOnly this solution did work for me. And I did not need add additional code of lines in the methods. I did use custom cells.
-
SAHM over 9 yearsWhat if we want to set the margin only for one type of cell in a grouped tableView?
-
chris stamper over 9 yearsYou 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.
-
mts over 9 yearsThis worked for me with the addition of
cell.preservesSuperviewLayoutMargins
in my data source method:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-
ArpitM over 9 yearsShouldn't you also pass the call to
super
inviewDidLayoutSubviews
? -
hufeng03 over 9 yearsFor 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.
-
Jakob over 9 yearshere is my observation (on iOS8): UITableViewController want's that solution. For TableViews inside UIViewController the accepted answer does the trick.
-
LunaCodeGirl over 9 yearsThank you! This really is the more concise answer. Much better than overriding two methods and brute forcing it.
-
GoodSp33d over 9 yearsThis deservers more up votes. It avoids all that OS specific code.
-
tsafrir over 9 yearsOverriding layoutMargins method is the most clean solution when using custom cells.
-
Petar over 9 yearsHow can this approach be used if the deployment target is set to iOS 7 ?
-
davetw12 over 9 yearsIn iOS 8, for me, this wouldn't work without
[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
-
chris stamper over 9 yearsYou 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.
-
Mike Abdullah over 9 years@davetw12 and @cdstamper Can you provide any evidence of this? All my explorations so far say that setting both
layoutMargins
andpreservesSuperviewLayoutMargins
on the cell is all that is required. Anything else feels like trusting in voodoo. -
chris stamper over 9 yearsThis 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 over 9 yearsNo. 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.
-
Mike Abdullah over 9 yearsOK. If you can provide some sort of test case, I'd be delighted to dig into it.
-
davetw12 over 9 yearsNo, it's not a form of voodoo @MikeAbdullah. In order for you to avoid the default
separatorInset
of aUITableViewCell
in iOS 8, you have to set the set theUIEdgeInsets
to zero on both theUITableView
and theUITableViewCell
. If you don't do both, you'll still have a left inset that is greater than zero. I have confirmed this multiple times. -
Mike Abdullah over 9 years@davetw12 Not according to my findings. Setting
preservesSuperviewLayoutMargins
toNO
overrides that inherited margin -
King-Wizard over 9 years"Setup your cell margins" step using iOS 8.1 and Swift was enough for me to make it work.
-
King-Wizard over 9 yearsFor the Swift version, working and tested on iOS 7 and 8, please see my answer below this post.
-
Ivan over 9 yearsWhat 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.
-
Lily Ballard over 9 yearsTesting with iOS 8.1 in the simulator, setting
layoutMargins
andpreservesSuperviewLayoutMargins
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 setseparatorInset
toUIEdgeInsetsZero
then the cell separators are inset but the "extra" ones aren't. -
Mike Abdullah over 9 yearsAh, I hadn't considered "extra" separators, as I've been focusing on grouped style tables. Interesting to know, thanks, Kevin!
-
Abdullah Umer over 9 yearsI put the code for tableView in viewDidLoad and are working fine. I think its not necessary to call them every time in willDisplayCell().
-
esh about 9 years
preservesSuperviewLayoutMargins
isNO
by default. -
Mike Abdullah about 9 years@BlackFlam3 it isn't in my experience. Are you talking about
UIView
generally, or a specific kind of view? -
esbenr almost 9 yearsOverriding layoutMargins method worked (iOS 8.3), setting property was not.
-
Rivera almost 9 yearsYou 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. -
NSDeveloper almost 9 years[self.tableView setSeparatorInset:UIEdgeInsetsZero]; will make section title no inset in grouped tableView.
-
Droid GEEK over 8 yearsSame 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 }}
-
DawnSong over 8 years
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 over 8 years
tableView.layoutMargins = UIEdgeInsetZero;
trigers layout and dataSource methods being called immediately. So your solution is preferred. -
gklka over 8 yearsNote: it did not work me on
UITableViewCell
descentants, only on the main class. -
Christian over 8 yearsit is funny to add some code for each version so that the initial appearance stays the same...
-
Martin Berger over 8 yearsThis worked for me:
UIEdgeInsetsMake(0, CGRectGetWidth(self.bounds)/2.0, 0, CGRectGetWidth(self.bounds)/2.0)
. Taken from here -
derpoliuk over 8 yearsAnd you can do this in storyboard: Size Inspector (
cmd+alt+5
) -> Layout Margins section. -
Mohamed Saleh over 8 yearsPlease 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 -
Sergiu Todirascu about 8 yearsThis 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.
-
saintjab almost 8 yearscalled this in cellForRowAtIndexPath delegate method. Good answer :)
-
zirinisp almost 8 years@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.
-
David Pettigrew over 7 yearsIt 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?
-
Leon almost 7 yearsOverriding 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.
-
SAHM over 6 yearsDo you happen to have an update for this answer for iOS 11? It no longer works.