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.
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.
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
}
}
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];
}
}
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
!
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.
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:
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
Things to note:
preservesSuperviewLayoutMargins
if desired.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.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!)