How to set the full width of separator in UITableView
I have a UITableView
where the separators don't have the full width. It ends like 10 pixels before the left side. I was playing around with this code in the viewDidLoad()
.
self.tableView.layoutMargins = UIEdgeInsetsZero;
Also in the storyboard when you can select custom or default selectors. Now all the cells that are populated don't have the full-width selectors but the cells that are empty have full width.
How can I fix this?
This worked for me on iOS 8.4 - 9.0 devices using Xcode 6.4 and Swift 1.2:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
return cell
}
Swift 5 Update:
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
In your UITableViewCell
Go to Attributes Inspector in your Interface Builder and simply change "15" to 0. Do this for all the cells you wish to change.
You may need to add [cell setLayoutMargins:UIEdgeInsetsZero];
to your tableViewCell
I got the answer from this post: iOS 8 UITableView separator inset 0 not working
Just add this code on your UITableViewController
-(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];
}
}
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
for Swift 3 :
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorInset = .zero
tableView.layoutMargins = .zero
}