I am setting up a UITableView programmatically. I would like the content of the cell to span the entire width of the screen. I have successfully set the cell to span the width of the screen, but the content and separators are still inset significantly (iPad screenshot below).
Here is the tableview layout setup in my view controller implementation:
- (void) viewDidLoad {
[super viewDidLoad];
// table layout
self.tableView.rowHeight = 192;
UILayoutGuide *margins = [self.view layoutMarginsGuide];
[self.tableView.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor] ;
[self.tableView.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor];
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
CGRect tableRect = self.view.frame;
self.tableView.frame = tableRect;
// table colors
self.tableView.backgroundColor = [UIColor grayColor];
self.tableView.separatorColor = [UIColor grayColor];
UIView *backView = [[UIView alloc] init];
[backView setBackgroundColor:[UIColor grayColor]];
[self.tableView setBackgroundView:backView];
}
Then I set the cell's content:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor blueColor];
cell.indentationWidth = 0;
cell.indentationLevel = 0;
cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0);
cell.contentView.backgroundColor = [UIColor purpleColor];
return cell;
}
The cell background is blue, and it spans the width of the screen. The purple area in my screenshot is the contentView, and as you can see, it doesn't stretch to the right edge of the screen, and the cell text is inset at the left. The separator is also inset at left and right.