LoginSignup
23
24

More than 5 years have passed since last update.

UITableView の区切り線(separator)を端まで伸ばす

Posted at

UITableView のセルの区切り線(Separator)をテーブルの端まで伸ばすTIPS

いつもどおりStoryboard から separetorinsetいじって終わるかとおもいきや終わらず
少しハマったのでメモ

どうやらiOS8で追加されたマージンも合わせて調節しないといけないようだ。

willDisplayCell
// セパレータの設定
-(void)tableView:(UITableView *)tableView
 willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath{

    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

上記処理を追加したらできた。

下記2つのプロパティを調整している
layoutMargins は自分自身のサブビューとのマージンを

preservesSuperviewLayoutMargins はlayoutMargins の値をスーパービューから引き継ぐかどうかの真偽値を表している。

どちらもiOS8から追加されたUIView のプロパティのようだ。
セルのスーパービューからサブビューとの間のマージンを引き継いでしまっているため、
それを無効化しないとセパレータがマージンに影響されてしまうようだ。

参考:iOS 8 UITableView separator inset 0 not working

23
24
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
23
24