Why UITableView cell separatorInset is default to 15, when view controller's root view Layout Margins is 16?

老子叫甜甜 提交于 2019-12-11 11:13:02

问题


On iPhone 6s, when I set cell separator line left inset to 14, is show 15, I think that is because it set preservesSuperviewLayoutMargins = true by default.

But from apple doc: "The side margins vary depending on how and where the controller is presented, but can be either 16 or 20 points(depending on the device). You cannot change these margins."

When I add a view to the side of root view by auto layout, and set preservesSuperviewLayoutMargins = true, the distance from the edge of root view to the view I added is 16 on iPhone 6s, so it is not aligned to the left edge of the table view. How can UITableViewCell use preservesSuperviewLayoutMargins = true, and the side margins is 15, not 16?


回答1:


After some logs, I know why, that is because:

tableView.preservesSuperviewLayoutMargins = false

And the table View's layoutMargins is (8, 15, 8, 15) on iPhone 6; (8, 20, 8, 20) on iPhone 6 Plus.

On apple documents, it is only said: "The default margins are 8 points on each side. You can modify these margins based on your app’s needs."

But the table view's layoutMargins is (8,8,8,8) on viewDidLoad(), then change to (8, 15, 8, 15) later on iPhone 6.

This is very confusing when I want to add a view left align to the table view, and the subview's left edge left align to table view separator line. I had to update the view like this:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    totalBar.preservesSuperviewLayoutMargins = tView.preservesSuperviewLayoutMargins
    totalBar.layoutMargins = tView.layoutMargins
}

So that I don't need to hardcode the margin values for 6 and 6 Plus, and for even Apple change the value in future.



来源:https://stackoverflow.com/questions/36982576/why-uitableview-cell-separatorinset-is-default-to-15-when-view-controllers-roo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!