LoginSignup
43
38

More than 5 years have passed since last update.

UITableViewのサイズを内容に応じて縮める

Last updated at Posted at 2014-03-18

全画面でUITableViewを表示しているが、表示するデータが少ない際は空のセルを表示せず、テーブル背後のビュー等を見せたい場合があった。

(変更前)
iOSシミュレータのスクリーンショット 2014.03.18 8.46.35.png

表示する内容(=contentSize)に応じてテーブルを伸縮させればよい。


// テーブルロード後に以下を実行すればよい.
- (void)updateTableSize:(UITableView *)tableView
{
    tableView.frame =
    CGRectMake(tableView.frame.origin.x,
               tableView.frame.origin.y,
               tableView.contentSize.width,
               MIN(tableView.contentSize.height,
                   tableView.bounds.size.height));
}

// 例.
- (void)hoge
{
    [_tableView reloadData];
    [self updateTableSize:_tableView];
}

(変更後)
iOSシミュレータのスクリーンショット 2014.03.18 8.51.12.png

43
38
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43
38