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?
Swift version
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:
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:
...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.
Swift 3.0 example:
In iOS8:
Adding this to my UITableViewCell Subclass:
and this to "tableView:cellForRowAtIndexPath" or "tableView:willDisplayCell":
WORKED for me.
Here's an easy way to globally remove the inset.
In
UITableViewCell+Extensions.swift
:In
AppDelegate
application:didFinishLaunchingWithOptions:
:You might think to a) also just override
separatorInset
in the extension, or b) set the appearance proxy forlayoutMargins
, instead. Neither will work. Even thoughseparatorInset
is indicated to be a property, attempting to override it as a property (or method) generates compiler errors. And setting the appearance proxy forUITableViewCell
'slayoutMargins
(or, for that matter, also setting the appearance proxies forUITableView
'slayoutMargins
andseparatorInset
) has no effect.Adding this snippet, simple elegant in Swift works for me in iOS8 :)
You can use UIAppearance once, at your application startup (before UI is loaded), to set it as default global settings:
This way, you keep your UIViewController's code clean and can always override it if you want.