この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
separatorInsetを設定しても、iOS 8では反映されない
UITableViewの区切り線を左端まで表示したい場合、下記設定を行うと思いますが、 iOS 8では反映されない現象が発生しました。
self.tableView.separatorInset = UIEdgeInsetsZero;
UITableViewのlayoutMarginsを設定する
iOS 8から新しく追加されたlayoutMarginsを設定すると現象が解決できました。
ただし、UITableViewCellにlayoutMarginsを設定する場合は、UITableViewCellのpreservesSuperviewLayoutMarginsをfalseにする必要がありました。
override func viewDidLoad() {
super.viewDidLoad()
if self.tableView.respondsToSelector("separatorInset") {
self.tableView.separatorInset = UIEdgeInsetsZero;
}
if self.tableView.respondsToSelector("layoutMargins") {
self.tableView.layoutMargins = UIEdgeInsetsZero;
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
・・・
if cell.respondsToSelector("separatorInset") {
cell.separatorInset = UIEdgeInsetsZero;
}
if cell.respondsToSelector("preservesSuperviewLayoutMargins") {
cell.preservesSuperviewLayoutMargins = false;
}
if cell.respondsToSelector("layoutMargins") {
cell.layoutMargins = UIEdgeInsetsZero;
}
return cell
}
まとめ
preservesSuperviewLayoutMarginsがtrueの場合、親のViewのlayoutMarginsに合わせる為、layoutMarginsの設定が正常に反映されないと理解しています。
iOS 8対応で区切り線を左端まで表示したい場合、上記修正方法をお試しください!