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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26107188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:54:33  来源:igfitidea点击:

How do I adjust the left margin for prototype cells in a UITableView?

iosobjective-cuitableviewinterface-builder

提问by j b

If I create a UITableViewController, for example via File → New Project... → iOS → Master-Detail Applicationin Xcode, a UITableViewis created with a prototype cell.

如果我创建一个UITableViewController,例如通过File → New Project... → iOS → Master-Detail Applicationin Xcode,aUITableView是用原型单元创建的。

The generated view hierarchy is:

生成的视图层次结构是:

UITableViewController view hierarchy

UITableViewController 视图层次结构

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

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

Prototype cell gap

原型细胞间隙

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

这会在UILabel运行时在设备的屏幕边缘和文本之间产生相应的边距:

Runtime gap

运行时间隙

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

那么,这个间隙集的宽度在哪里,如何调整呢?

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

UILabel 的 Size Inspector 中的控件显示为灰色:

enter image description here

在此处输入图片说明

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.

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

采纳答案by MilanPanchal

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

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

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

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

OUTPUT RESULT

输出结果

回答by Farhan C K

Go to Main.storyboard> select the UITableViewCell> Attributes Inspector. Change Separator dropdown list from Default Insets to Custom Insets. Change the left inset from 15 to 0

转到Main.storyboard> 选择UITableViewCell> Attributes Inspector。将分隔符下拉列表从默认插入更改为自定义插入。将左侧插图从 15 更改为 0

enter image description here

在此处输入图片说明

回答by Francesco Vadicamo

Starting from iOS 8is available the cell property layoutMargins. So the correct way to adjust cell margins is setting this property in your tableView:cellForRowAtIndexPathor in your custom UITableViewCellin this way:

iOS 8开始可以使用 cell 属性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
}

I hope this can help someone.

我希望这可以帮助某人。

回答by Chetan

Just add the method in your code as mentioned in the below link

只需在您的代码中添加方法,如以下链接中所述

How do I eliminate the margin on the left side of a UITableView, without creating a gap on the right?

如何消除 UITableView 左侧的边距,而不会在右侧产生间隙?

The method is like:

方法是这样的:

-(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];
    }
}

Swift 3 and 4

斯威夫特 3 和 4

public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        cell.separatorInset = .zero
    }
    if (cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins))) {
        cell.preservesSuperviewLayoutMargins = false
    }

    if (cell.responds(to: #selector(setter: UIView.layoutMargins))) {
        cell.layoutMargins = UIEdgeInsets.zero
    }
    if tableView.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        tableView.separatorInset = .zero
    }

}

回答by Alessandro Francucci

If you're using a xib for your cell, be sure that the "Relative to margin" is unchecked. You can check this via the inspector as shown in the following screenshot:

如果您在单元格中使用 xib,请确保未选中“相对于边距”。您可以通过检查器进行检查,如下面的屏幕截图所示:

enter image description here

在此处输入图片说明

回答by Nicolai Nita

In the TableView "Attributes inspector" set the Separator Insets to "Custom" with Left = 0. That is all you have to do!

在 TableView 的“属性检查器”中,将 Separator Insets 设置为“Custom”,Left = 0。这就是你所要做的!

回答by Dannie P

As WTIFS mentioned, UITableViewCell's indentationproperty is a great way to indent text labels in a built-in cell styles. Just do something like this to achieve nice left margin:

正如 WTIFS 所提到的,UITableViewCellindentation属性是在内置单元格样式中缩进文本标签的好方法。只需执行以下操作即可获得漂亮的左边距:

cell.indentationLevel = 2;

This, however, would not work for imageView.

但是,这不适用于 imageView。

回答by Paul Lehn

Here is the swift version of Chetan's answer:

这是 Chetan 答案的快速版本:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        tableView.separatorInset = UIEdgeInsets.zero
        tableView.layoutMargins = UIEdgeInsets.zero
        cell.preservesSuperviewLayoutMargins = false
        cell.layoutMargins = UIEdgeInsets.zero
        cell.separatorInset = UIEdgeInsets.zero
}

回答by Naishta

And if you want to change just the left margin of the cell's textlabel only, then change the Horizontal Space Constraint 'Constant'to say 16 or 8 based on the padding you want.(This is in the nib file). If you cant get to the 'constant' Select the label, change the x coordinate in the FrameRectangle View, and then click on the constraint pin at the left) enter image description here

如果您只想更改单元格文本标签的左边距,则Horizontal Space Constraint 'Constant'根据您想要的填充将 the 更改为 16 或 8。(这是在 nib 文件中)。如果不能到'constant' 选择label,在FrameRectangle View中改变x坐标,然后点击左边的constraint pin) 在此处输入图片说明

回答by WTIFS

I think I just came up with an easy solution. lol.

我想我只是想出了一个简单的解决方案。哈哈。

The top answer have some problem...it decrease the left gap, but resulting a right gap.

最佳答案有一些问题......它减少了左边的差距,但导致了右边的差距。

I used the constrainsin Interface Builder.

我在 Interface Builder 中使用了约束

First add a -15 left margin constrainto the Table View.

首先向表格视图添加-15 左边距约束

Then add some Indentationto the Table Cell to make the contents look better.

然后在表格单元格中添加一些缩进,使内容看起来更好。



Here're some step-by-step pics:

以下是一些分步图片:

Add the constraint. Remember to uncheck the "spacing to nearest neighbor".

添加约束。请记住取消选中“与最近邻居的间距”。

Add the constraint. Remember to uncheck the "spacing to nearest neighbor".

添加约束。 请记住取消选中“与最近邻居的间距”。

The Table Cells will move left. But seems too close to margin.

表格单元格将向左移动。但似乎太接近保证金了。

After adding the constraint

添加约束后

So choose the Table Cell, and add some indentation in the right area.

所以选择表格单元格,并在正确的区域添加一些缩进。

add some indentation

添加一些缩进