iOS 8
UITableView
separator inset
troubleshooting
app development

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

Understanding the Problem with iOS 8 UITableView Separator Inset

When developing applications on iOS 8 using UITableView, developers encountered an unexpected behavior where setting separatorInset to zero did not eliminate the insets as expected. This issue perplexed many developers because the exact behavior was different in previous iOS versions. Below, we'll delve into the technicalities of this problem and explore potential workarounds.

Technical Explanation

The UITableView class in iOS is used extensively for creating scrollable lists. A common requirement is to customize the separator lines between cells for aesthetics or alignment reasons. The separator insets define the space from the edge of the cell to where the separator begins and ends.

Original Expectation

In earlier iOS versions, setting:

swift
tableView.separatorInset = UIEdgeInsets.zero

would remove any padding or margins around the separator lines, allowing them to stretch edge-to-edge within the table view.

iOS 8 Behavior

With iOS 8, the behavior changed due to the introduction of layout margins. The new layout system inherently respects the layout margins of the superview, and by default, UITableViewCell respects these margins. This caused the zero insets to not behave as expected, since the layout margins also influence separator insets.

Investigating the Underlying Cause

At the heart of this issue is the addition of the layoutMargins property, which by default, is non-zero. It affects how the separator inset is drawn. The separatorInset effectively becomes a combination of the table view cell's layout margins plus any additional insets specified by separatorInset.

Solutions

To address this unexpected behavior, a few solutions are proposed:

  1. Override Default Layout Margins:
swift
   if #available(iOS 8.0, *) {
       tableView.layoutMargins = UIEdgeInsets.zero
   }
  1. Disable Cell's Layout Margins: Ensure your cells conform by setting them directly:
swift
   cell.layoutMargins = UIEdgeInsets.zero
  1. Subclass UITableViewCell: For a more permanent and reusable solution, subclass UITableViewCell and override layoutMargins:
swift
1   class CustomTableViewCell: UITableViewCell {
2       override func layoutSubviews() {
3           super.layoutSubviews()
4           self.layoutMargins = UIEdgeInsets.zero
5       }
6   }
  1. Separator Inset Property: Override the cell's separator inset if needed:
swift
   cell.separatorInset = UIEdgeInsets.zero

Key Solutions Summary

Solution TypeCode ExampleNotes
Override Layout MarginstableView.layoutMargins = UIEdgeInsets.zeroWorks for entire table view; iOS 8+
Set Cell Layout Marginscell.layoutMargins = UIEdgeInsets.zeroEnsures each cell respects margin
Subclass UITableViewCellSee code snippet aboveMore robust and reusable solution
Direct Separator Inset Overridecell.separatorInset = UIEdgeInsets.zeroDirect adjustment; check efficacy

Additional Considerations

  • Autolayout: If using AutoLayout, ensure constraints are correctly set, as AutoLayout might inadvertently affect your layout margins.
  • Backward Compatibility: If supporting older iOS versions, the segmented approach where code execution is conditional on iOS version checks (#available(...)) is crucial.
  • Alternative UI/ Frameworks: Consider other UI solutions or third-party frameworks that might offer enhanced control over separators and insets, although this should be weighed against additional complexity.

Conclusion

The iOS 8 changes to UITableView separator behavior centered around the new layout margin system, requiring specific handling strategies to ensure desired UI appearance. By understanding these changes and employing the right techniques, developers can overcome these challenges and achieve precise control over their application's UI elements.


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.