ios 如何在 UITableView 中设置分隔符的全宽

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26519248/
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 03:18:10  来源:igfitidea点击:

How to set the full width of separator in UITableView

iosobjective-ciphoneuitableview

提问by Timo Cengiz

I have a UITableViewwhere the separators don't have the full width. It ends like 10 pixels before the left side. I was playing around with this code in the viewDidLoad().

我有一个UITableView分隔符没有全宽的地方。它在左侧前 10 个像素处结束。我在viewDidLoad().

self.tableView.layoutMargins = UIEdgeInsetsZero;

Also in the storyboard when you can select custom or default selectors. Now all the cells that are populated don't have the full-width selectors but the cells that are empty have full width.

同样在故事板中,您可以选择自定义或默认选择器。现在填充的所有单元格都没有全宽选择器,但空的单元格具有全宽。

How can I fix this?

我怎样才能解决这个问题?

采纳答案by Timo Cengiz

I got the answer from this post: iOS 8 UITableView separator inset 0 not working

我从这篇文章中得到了答案:iOS 8 UITableView separator inset 0 not working

Just add this code on your UITableViewController

只需将此代码添加到您的 UITableViewController

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

回答by MB_iOSDeveloper

This worked for me on iOS 8.4 - 9.0 devices using Xcode 6.4 and Swift 1.2:

这在使用 Xcode 6.4 和 Swift 1.2 的 iOS 8.4 - 9.0 设备上对我有用:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = UITableViewCell()


    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsetsZero
    cell.layoutMargins = UIEdgeInsetsZero

    return cell
}

Swift 5 Update:

斯威夫特 5 更新:

cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero

回答by HannahCarney

In your UITableViewCell

在你的 UITableViewCell

Go to Attributes Inspector in your Interface Builder and simply change "15" to 0. Do this for all the cells you wish to change.

转到界面生成器中的 Attributes Inspector 并将“15”更改为 0。对您希望更改的所有单元格执行此操作。

instets

插入

You may need to add [cell setLayoutMargins:UIEdgeInsetsZero];to your tableViewCell

您可能需要添加[cell setLayoutMargins:UIEdgeInsetsZero];到您的tableViewCell

回答by Wilson

for Swift 3 :

对于 Swift 3 :

override func viewDidLoad() {
  super.viewDidLoad()

  tableView.separatorInset = .zero
  tableView.layoutMargins = .zero
}

回答by Sebastián Lara

  1. Select your UITableViewCell
  2. Go to Atributes Inspector
  3. Go to Separatorand change it to "custom Insets"
  4. Set leftand / or rightfields. (By default left: 15, right: 0)
  1. 选择您的 UITableViewCell
  2. 转到属性检查器
  3. 转到分隔符并将其更改为“自定义插入”
  4. 设置left和/或right字段。(默认情况下left: 15right: 0

Look how it works (using left: 100):

看看它是如何工作的(使用left: 100):

enter image description here

在此处输入图片说明

Result:

结果:

enter image description here

在此处输入图片说明

回答by H6.

I inherit from UITableViewControllerand needed additional to the two insets settings in willDisplayCellalso to set preservesSuperviewLayoutMarginsto false. In Swift it looks like this:

我继承UITableViewController并需要额外的两个插图设置,willDisplayCell也设置preservesSuperviewLayoutMargins为 false。在 Swift 中,它看起来像这样:

override func tableView(_tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    if cell.respondsToSelector("setSeparatorInset:") {
        cell.separatorInset = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setLayoutMargins:") {
        cell.layoutMargins = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
        cell.preservesSuperviewLayoutMargins = false
    }
}

回答by Ramin

The Separator Insetis by default 15 from left. Change Separator Insetoption from autoto customand set the inset to 0.

分隔符Inset默认为左起 15 个。将 SeparatorInset选项从更改autocustom并将插图设置为0

enter image description here

在此处输入图片说明

回答by Michael Rose

For people having issues with the iPad — this will get you in a state that is the same as the iPhone. You can then adjust the separatorInsetas needed.

对于 iPad 有问题的人——这会让你处于与 iPhone 相同的状态。然后,您可以separatorInset根据需要进行调整。

tableView.cellLayoutMarginsFollowReadableWidth = false

回答by Christopher Larsen

For Swift in iOS 9+

适用于 iOS 9+ 中的 Swift

If using a custom UITableViewCell:

如果使用自定义UITableViewCell

override var layoutMargins: UIEdgeInsets {
    get { return UIEdgeInsetsZero }
    set(newVal) {}
}

then for your UITableViewin viewDidLoad:

那么对于你的UITableViewin viewDidLoad

self.tableView?.separatorInset = UIEdgeInsetsZero;
self.tableView?.layoutMargins = UIEdgeInsetsZero;

回答by Aks

Use it in cellForRowAtIndexPathmethod, to configure cell's separator specs ,
it works perfect for iOS9.0+

cellForRowAtIndexPath方法中使用它,配置单元格的分隔符规格,
它适用于iOS9.0+

 cell.separatorInset = UIEdgeInsetsZero;
 cell.layoutMargins = UIEdgeInsetsZero;
 cell.preservesSuperviewLayoutMargins = NO;