iOS
UITableView
separator inset
iOS 8
debugging

iOS 8 UITableView separator inset 0 not working

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

iOS 8 introduced many changes and updates to the UIKit framework, one of which was a change in behavior regarding UITableView. Developers noticed that setting UITableView separator insets to zero didn't work as expected, causing a lot of confusion and frustration in the community. This article explores the details of this issue, providing technical explanations and potential workarounds.

Background on UITableView Separator Insets

UITableView is a key component used to display lists in iOS applications. It includes a feature to customize separator insets, which control the space around the cell separators. Prior to iOS 8, developers could set these separator insets to zero, aligning separators with the edges of the screen.

Problem Introduction in iOS 8

With the release of iOS 8, developers noticed that setting the separator insets to zero did not produce the expected results. Instead of aligning the separators to the full width of the screen, there appeared to be default padding that could not be removed by simply setting the insets to zero.

Technical Explanation

The issue arose because iOS 8 introduced a change in the default behavior of insets due to updates in the underlying layout system and its content layout guides. The introduction of automatic layout constraints meant UITableViewCell's actual layout margins might not reflect direct changes to the separator insets.

Code Example

Developers often tried setting the insets as follows:

objective-c
cell.separatorInset = UIEdgeInsetsZero;

However, this produced inconsistent outcomes, leading to unexpected spacing around the separators.

Workaround Solutions

While a direct fix wasn't immediately available, developers found a few workarounds to circumvent this issue:

Setting layoutMargins for iOS 8 and Above

One approach was to adjust the layoutMargins of the cell’s content view:

objective-c
1if ([cell respondsToSelector:@selector(preservesSuperviewLayoutMargins)]) {
2    cell.preservesSuperviewLayoutMargins = NO;
3}
4if ([cell respondsToSelector:@selector(layoutMargins)]) {
5    cell.layoutMargins = UIEdgeInsetsZero;
6}

Utilizing tableView:willDisplayCell:forRowAtIndexPath:

Another workaround involved overriding the tableView:willDisplayCell:forRowAtIndexPath: method to reset the separator insets manually:

objective-c
1- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
2    if ([cell respondsToSelector:@selector(separatorInset)]) {
3        cell.separatorInset = UIEdgeInsetsZero;
4    }
5    if ([cell respondsToSelector:@selector(preservesSuperviewLayoutMargins)]) {
6        cell.preservesSuperviewLayoutMargins = NO;
7    }
8    cell.layoutMargins = UIEdgeInsetsZero;
9}

Disabling preservesSuperviewLayoutMargins

Altering the behavior of superview layout margins by setting preservesSuperviewLayoutMargins to NO was crucial:

objective-c
cell.preservesSuperviewLayoutMargins = NO;

Summary Table

Here's a concise summary of key points and solutions:

IssueExplanationSolutions
Separator Insets IssueiOS 8 changed default insets, causing unwanted paddingUse layout margins and display methods
layoutMarginsAffects the cell's separator inset behaviorSet to UIEdgeInsetsZero
preservesSuperviewLayoutMarginsControls inheritance of insets from superviewSet to NO

Additional Considerations

Compatibility with Different iOS Versions

When implementing such workarounds, it's essential to ensure compatibility across different iOS versions. Use conditional checks to apply settings only when appropriate methods or properties are available.

Explore Further Customization

iOS development evolves continuously, and developers can make use of additional customization tools like custom cell classes or adjusting through Interface Builder.

Ongoing Monitoring

Always keep an eye on new updates and documentations from Apple, as future iOS versions might introduce fixes or alter existing behavior. Research and community forums are invaluable for staying updated on best practices.

In conclusion, while iOS 8 introduced a quirk with UITableView separator insets, understanding the new layout system and employing workarounds allows developers to maintain desired UI design effectively.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.