6

プログラムで 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 です。ご覧のとおり、画面の右端まで伸びておらず、セル テキストが左側に挿入されています。セパレーターも左右に挿入されています。

テーブルのスクリーンショット

4

2 に答える 2

4

@technerd による私の質問へのコメントのおかげで、この問題を発見しました。ありがとうございました!

私は iOS 9.2 でアプリをテストしていましたが、デフォルトでセル レイアウトを調整する新しい iOS9+ セル プロパティcellLayoutMarginsFollowReadableWidthを考慮していませんでした。

自動サイズ変更をオフにするには、@technerd が示すように、iOS のバージョンを確認してからプロパティを無効にする必要があります。

オブジェクティブ C

- (void)viewDidLoad {
    [super viewDidLoad];

    //For iOS 9 and Above 

    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0) {
        self.tableView.cellLayoutMarginsFollowReadableWidth = NO;
    }
}

迅速

override func viewDidLoad() {
    super.viewDidLoad()

    //For iOS 9 and Above 
    if #available(iOS 9, *) {
        tableView.cellLayoutMarginsFollowReadableWidth = false
    }
}

これが他の人に役立つことを願っています。

于 2016-01-29T18:42:26.623 に答える