如何在UITableView中调整原型单元格的左边距?

问题描述:

如果我创建 UITableViewController ,例如通过Xcode中的文件→新建项目...→iOS→主从应用程序,使用原型单元格创建 UITableView

If I create a UITableViewController, for example via File → New Project... → iOS → Master-Detail Application in Xcode, a UITableView is created with a prototype cell.

生成的视图层次结构为:

The generated view hierarchy is:

在Cell的内容 UIView 左边缘和标题文本的 UILabel 元素之间自动创建左边距如下所示,橙色。

A left "margin" is automagically created between the Cell's Content UIView left edge and the "Title" text's UILabel element as shown below in orange.

这会导致设备的屏幕边缘与运行时的 UILabel 文本之间存在相应的差距:

This results in a corresponding margin between the device's screen edge and the UILabel text at runtime:

那么,这个差距的宽度设置在哪里,以及如何调整?

So, where is the width of this gap set, and how can it be adjusted?

UILabel的Size Inspector中的ntrol灰色显示:

The controls in the Size Inspector for the UILabel are greyed out:

我首选的选项是能够在Interface Builder中设置此间隙的宽度,但我还想了解这个位置正在设置间隙,以及如何以编程方式更改它。

My preferred option would to be able to set the width of this gap from within Interface Builder, but I would also like to understand where this gap is being set, and how to alter it programmatically.

您只需要设置 contentInset 表视图的属性。您可以根据需要设置值。

You just need to set contentInset property of the table view. You can set value according to your need.

self.tableView.contentInset = UIEdgeInsetsMake(0,-15,0,0) ;

self.tableView.contentInset = UIEdgeInsetsMake(0, -15, 0, 0);

输出结果