プログラムで UITableView を設定しています。セルのコンテンツが画面の幅全体に広がるようにします。セルを画面の幅全体に設定することに成功しましたが、コンテンツとセパレーターはまだ大きく挿入されています (下の iPad スクリーンショット)。
私のView Controller実装でのテーブルビューレイアウトの設定は次のとおりです。
- (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];
}
次に、セルのコンテンツを設定します。
- (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;
}
セルの背景は青で、画面の幅全体に広がります。スクリーンショットの紫色の領域は contentView です。ご覧のとおり、画面の右端まで伸びておらず、セル テキストが左側に挿入されています。セパレーターも左右に挿入されています。