Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TableView ContentInset does not shrink cell width / Add horizontal padding to TableView contents

I'd like to have a UITableView which is full screen. But the content of the UITableView should have a padding on the left and right.

So I tried to set ContentInset. But now the cells are as wide as the UITableView and the UITableView scrolls horizontally.

Is there a way to say that the UITableView content's width should become narrowed by the horizontal content insets? Or do I have to add the padding to all cells and header/footer views?

I don't want to narrow the table view itself, because the scroll indicator should stay at the right side of the screen and not in the middle. The here (How to set the width of a cell in a UITableView in grouped style) suggested solution seems to be not as generic as i'd love to, beacuse the cells and header and footer views have to know about the padding (at least 3 places to maintain instead of one)

like image 684
Sven-Michael Stübe Avatar asked Jul 22 '14 08:07

Sven-Michael Stübe


2 Answers

I don't want to narrow the table view itself, because the scroll indicator should stay at the right side of the screen and not in the middle.

This makes you happy?

_tableView.clipsToBounds = NO;
_tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -30.f);

If you don't like clipsToBounds = NO effects, you can embed the tableView in container view which is clipsToBounds = YES.

like image 170
rintaro Avatar answered Oct 24 '22 05:10

rintaro


Set the layout margins of the table view. For this to work make sure your constraints in the cells are set relative to the superview margin.

tableView.layoutMargins = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 40)

enter image description here

like image 32
Balázs Vincze Avatar answered Oct 24 '22 07:10

Balázs Vincze