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.
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:
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:
- Override Default Layout Margins:
- Disable Cell's Layout Margins: Ensure your cells conform by setting them directly:
- Subclass UITableViewCell: For a more permanent and reusable solution, subclass
UITableViewCelland overridelayoutMargins:
- Separator Inset Property: Override the cell's separator inset if needed:
Key Solutions Summary
| Solution Type | Code Example | Notes |
| Override Layout Margins | tableView.layoutMargins = UIEdgeInsets.zero | Works for entire table view; iOS 8+ |
| Set Cell Layout Margins | cell.layoutMargins = UIEdgeInsets.zero | Ensures each cell respects margin |
| Subclass UITableViewCell | See code snippet above | More robust and reusable solution |
| Direct Separator Inset Override | cell.separatorInset = UIEdgeInsets.zero | Direct 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
- iOS 8 UITableView separator inset 0 not working
- iOS 9 “fbauth2” missing from Info.plist
- iOS 9 not opening Instagram app with URL SCHEME
- iOS 9 not opening Instagram app with URL SCHEME
- iOS 9 Xcode 7 - Application appears with black bars on top and bottom
- iOS – Run/Debug/Install builds over Wi-Fi
- iOS / Android cross platform development
- iOS Access app-info.plist variables in code
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.