Resolving UITableView Separator Insets Issue in iOS 8 and Later

Resolving UITableView Separator Insets Issue in iOS 8 and Later

22 May 2024 Stephan Petzl Leave a comment Tech-Help

If you’ve been developing for iOS, you might have encountered an issue where the separator inset of a UITableView is not behaving as expected, particularly when migrating from iOS 7 to iOS 8. This article will guide you through the steps to resolve this problem effectively.

Understanding the Problem

In iOS 7, setting the separator inset to custom values, such as Right 0 and Left 0, works perfectly. However, in iOS 8, the separator inset defaults to 15 on the right, even if it is set to 0 in the XIB files. This discrepancy is due to new properties introduced in iOS 8 that are not present in iOS 7.

Solution Overview

The primary cause of this issue is the introduction of the layoutMargins property on cells and table views in iOS 8. To address this, you need to ensure that your code accommodates these new properties while maintaining backward compatibility with iOS 7.

Step-by-Step Solution

  1. Override the layoutMargins Property:

    Subclass your UITableViewCell and override the layoutMargins property to return UIEdgeInsetsZero.

    -(UIEdgeInsets)layoutMargins { 
        return UIEdgeInsetsZero;
    }

    For Swift:

    override var layoutMargins: UIEdgeInsets { 
        get { return .zero } 
        set { } 
    }
  2. Configure Cell Margins in willDisplayCell Method:

    Ensure that the cell’s layout margins are explicitly set, and it does not inherit the table view’s margin settings.

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        // Remove separator 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];
        }
        // Explicitly set your cell's layout margins
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    }

    For Swift:

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        // Remove separator inset
        if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
            cell.separatorInset = .zero;
        }
        // Prevent the cell from inheriting the Table View's margin settings
        if cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {
            cell.preservesSuperviewLayoutMargins = false;
        }
        // Explicitly set your cell's layout margins
        if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
            cell.layoutMargins = .zero;
        }
    }
  3. Additional Configuration for iOS 9 and Later:

    In iOS 9, you may need to set the cellLayoutMarginsFollowReadableWidth property to NO.

    if ([tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) {
        tableView.cellLayoutMarginsFollowReadableWidth = NO;
    }

    For Swift:

    if tableView.responds(to: #selector(setter: UITableView.cellLayoutMarginsFollowReadableWidth)) {
        tableView.cellLayoutMarginsFollowReadableWidth = false;
    }

Conclusion

By following the steps above, you can ensure that your UITableView’s separator insets are correctly set to 0, even in iOS 8 and later versions. This approach maintains backward compatibility with iOS 7, ensuring a consistent user experience across different iOS versions.

Streamlining Mobile App Testing with Repeato

While resolving issues like UITableView separator insets is crucial, testing your app thoroughly is equally important. This is where Repeato can be a game-changer. Repeato is a no-code test automation tool for iOS and Android that allows you to create, run, and maintain automated tests for your apps efficiently. With its computer vision and AI capabilities, Repeato ensures that your tests are quick to edit and run, enabling you to focus on creating a great product.

Whether you’re a mobile developer or a QA professional, Repeato offers the flexibility to handle test automation without deep technical expertise. This allows developers to delegate testing tasks to non-technical colleagues, ensuring a more streamlined workflow.

For more information, visit our documentation or download Repeato to get started.

Like this article? there’s more where that came from!