Any way to horizontally inset the cells of a UITableView?

Howdy,

Is it possible to inset the left and right edges of the cells contained within a UITableView? I initially thought that contentInsets would do the trick, but I can't to get this to work. More specifically, it seems that adjusting the contentInsets only affects the scrollable area rather than the area actually occupied by the cells. Are the cells not a part of the content?

The only other way I can think of doing this is by embedding the UITableView inside another UIScrollView, which feels like overkill. Is there a better way? Note that I do not want to adjust the bounds of the UITableView because I want the area occupied by the insets to remain scrollable.

Thanks,

smkuehnhold

Answered by Frameworks Engineer in 723343022

When using the .insetGrouped style, cells are inset on the leading & trailing edges from the table view by the leading and trailing values of the UITableView's directionalLayoutMargins. You can set custom layout margins on the table view to adjust the amount of these insets.

Accepted Answer

When using the .insetGrouped style, cells are inset on the leading & trailing edges from the table view by the leading and trailing values of the UITableView's directionalLayoutMargins. You can set custom layout margins on the table view to adjust the amount of these insets.

In the case of a custom cell, you could put your own view inside the cell's contentView, and add AutoLayout to inset your desired number of pixels. Then make the cells contentView.backgroundColor = .clear, and if needed also set the cell.backgroundColor = .clear. That should do the trick.

Happy Coding! Wouter

Any way to horizontally inset the cells of a UITableView?
 
 
Q