iOS 9 UITableView separators insets (significant left margin)
Okay, I have found out the solution. The only thing required for that is to set on the presenting instance of UITableView
that flag cellLayoutMarginsFollowReadableWidth
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
I wanted to find some reference in the documentation but it looks like it is not ready yet, only mentioned on diff page.
As the flag was introduced in iOS 9 for the backward compatibility you should add a check before trying to set it:
if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
{
myTableView.cellLayoutMarginsFollowReadableWidth = NO;
}
For Swift 2.0
you can use #available
to check iOS version.
if #available(iOS 9, *) {
myTableView.cellLayoutMarginsFollowReadableWidth = false
}
Moreover you need to compile it with Xcode 7
or above.
EDIT
Please keep in mind that this is the only required fix if your separators looked "fine" up to iOS 8, otherwise you need to change a bit more. You can find info how to do this already on SO.
If you want to do it in interface builder. The default separator inset is Automatic
. Change it to custom
by selecting the dropdown.
Swift 2.2 iOS 9.3
In viewDidLoad
tableView.cellLayoutMarginsFollowReadableWidth = false
In UITableViewDelegates
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector(Selector("setSeparatorInset:")){
cell.separatorInset = UIEdgeInsetsZero
}
if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
cell.preservesSuperviewLayoutMargins = false
}
if cell.respondsToSelector(Selector("setLayoutMargins:")){
cell.layoutMargins = UIEdgeInsetsZero
}
}
Perfect Solution upto iOS 9
In viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
//Required for iOS 9
if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0) {
self.testTableView.cellLayoutMarginsFollowReadableWidth = NO;
}
}
In TableViewDelegate methods add following code:
- (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 3.0 / 4.0
tableView.cellLayoutMarginsFollowReadableWidth = false
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
cell.separatorInset = UIEdgeInsets.zero
}
if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
cell.preservesSuperviewLayoutMargins = false
}
if cell.responds(to: #selector(setter: UIView.layoutMargins)) {
cell.layoutMargins = UIEdgeInsets.zero
}
}