DRYな備忘録

Don't Repeat Yourself.

UITableViewの左にある謎の余白を消したい

やることは3つ

  1. UITableViewのseparatorInsetをゼロにする
    • これはインターフェースビルダーからでも変更可能
  2. UITableViewCellのlayoutMarginsをゼロにする
  3. UITableViewCellのpreservesSuperviewLayoutMarginsを無効にする
func viewDidLoad() {
    super.viewDidLoad()

    // 1) TableViewのinsetをゼロにする
    reasonsTable.separatorInset = UIEdgeInsetsZero
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell()

    // 2) 自分自身のmarginをゼロにする
    cell.layoutMargins = UIEdgeInsetsZero
    // 3) superviewからmarginを引き継がない
    cell.preservesSuperviewLayoutMargins = false

    cell.textLabel?.textAlignment = .Center
    cell.textLabel?.text = self.reasons[indexPath.row].germanText
    return cell
}