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
The Separator Inset
is by default 15 from left. Change Separator Inset
option from auto
to custom
and set the inset to 0
.
In viewDidLoad
(tested iOS11 - swift 4.1)
try
tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
For people having issues with the iPad — this will get you in a state that is the same as the iPhone. You can then adjust the separatorInset
as needed.
tableView.cellLayoutMarginsFollowReadableWidth = false
None of these solutions work on the iPad, but I have come up with a solution that covers both devices:
With reusable cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
...[other code]...
[cell setLayoutMargins:UIEdgeInsetsZero];
[cell setSeparatorInset:UIEdgeInsetsZero];
return cell;
}
With non reusable cells:
- (void)removeSeparatorInset:(UITableView*)tableView{
NSArray *cells = [tableView visibleCells];
for (UITableViewCell *cell in cells){
[cell setLayoutMargins:UIEdgeInsetsZero];
[cell setSeparatorInset:UIEdgeInsetsZero];
}
}
-(void) viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[self removeSeparatorInset:self.tableView];
}
Just to expand on this approach:
@property(nonatomic) UIEdgeInsets separatorInset;
@property(nonatomic) UIEdgeInsets layoutMargins;
Both properties can be used by UITableView
& UITableViewCell
.
The latter is, in fact, a property of UIView
, which is a parent class of both UITableView
& UITableViewCell
.
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
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