j b*_*j b 55 objective-c interface-builder uitableview ios
如果我创建一个UITableViewController
,例如通过文件→新建项目...→iOS→在Xcode中的主要细节应用程序,UITableView
则创建一个原型单元格.
生成的视图层次结构为:
在单元格的内容UIView
左边缘和"标题"文本UILabel
元素之间自动创建左"边距" ,如下面橙色所示.
这会在设备的屏幕边缘和UILabel
运行时的文本之间产生相应的余量:
那么,这个间隙的宽度设置在哪里,以及如何调整?
UILabel的Size Inspector中的控件显示为灰色:
我首选的选项是能够在Interface Builder中设置此间隙的宽度,但我还想了解这个间隙的设置位置,以及如何以编程方式更改它.
mil*_*hal 49
您只需要设置contentInset
表视图的属性即可.您可以根据需要设置值.
self.tableView.contentInset = UIEdgeInsetsMake(0, -15, 0, 0);
输出结果
Far*_*C K 41
转到Main.storyboard>选择UITableViewCell> Attributes Inspector.将分隔符下拉列表从默认插入更改为自定义插入.将左侧插入从15更改为0
Fra*_*amo 37
从iOS 8开始可以使用单元属性layoutMargins
.因此,调整单元格边距的正确方法是以这种方式在您tableView:cellForRowAtIndexPath
或您的自定义UITableViewCell
中设置此属性:
override func awakeFromNib() {
super.awakeFromNib()
self.layoutMargins = UIEdgeInsetsZero //or UIEdgeInsetsMake(top, left, bottom, right)
self.separatorInset = UIEdgeInsetsZero //if you also want to adjust separatorInset
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助别人.
只需在代码中添加方法,如下面的链接所示
我想提到的方法是
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
cell.preservesSuperviewLayoutMargins = NO;
[cell setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
[cell setSeparatorInset:UIEdgeInsetsZero];
}
}
Run Code Online (Sandbox Code Playgroud)
在TableView"属性检查器"中,将分隔符插入设置为"自定义",使用Left = 0.这就是您所要做的!
正如 WTIFS 所提到的,UITableViewCell
的indentation
属性是在内置单元格样式中缩进文本标签的好方法。只需执行以下操作即可获得漂亮的左边距:
cell.indentationLevel = 2;
Run Code Online (Sandbox Code Playgroud)
但是,这不适用于 imageView。