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

时间:2015-01-14 07:34:53

标签: ios swift uitableview

我的目标是iOS 7和8,我想在下面的UITableView中消除出现在图像左侧的边距。如果解决方案简单/优雅,我甚至愿意接受仅适用于iOS 8的解决方案。 (因为它消失了,我只会忍受iOS 7上的边距)。

这就是我想要的样子:

我已经阅读了许多关于Stack Overflow(such as this one)的类似问题,或者特定于UITableViews分组的问题(herehere),但似乎无法完成任何工作对。

例如,如果我尝试使用contentInset解决问题(SO上的常见答案):

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.contentInset.top, -16, self.tableView.contentInset.bottom, self.tableView.contentInset.right);
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 33, 0, 0);
}

然后我最终得到了tableview右侧的差距:

我明白为什么会这样,整个tableview只是转移过来,如Debug View Hierarchy屏幕所示:

但是,我已经尝试调整tableView框架大小来补偿,但它似乎永远不会正常工作。加上这看起来很丑陋;必须有更好的方法。

记录中,这是我的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // get a cell that we can use]
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoCell" forIndexPath:indexPath];

    cell.textLabel.numberOfLines = 0;

    cell.selectedBackgroundView = selectionColor;

    cell.accessoryView = [[UIImageView alloc] initWithImage:[PTStyleKit imageOfArrow]];

    // Create the attributed string (text + attributes)
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:[videos objectAtIndex:indexPath.row] attributes:attributes];

    // Set it in our UILabel and we are done!
    [cell.textLabel setAttributedText:attributedText];

    NSString* filename = [filenames objectAtIndex:indexPath.row];
    if (filename == nil)
    {
        filename = cell.textLabel.text;
    }

    UIImage *imageOne = [UIImage imageNamed:[NSString stringWithFormat:@"medium-%@", filename]];
    cell.imageView.image = [UIImage imageWithCGImage:imageOne.CGImage scale:imageOne.size.height/44 orientation:imageOne.imageOrientation];

    return cell;
}

6 个答案:

答案 0 :(得分:4)

为表格视图设置清晰的背景并设置contentInset属性。

<强>代码:

tableView.contentInset = UIEdgeInsetsMake(0, -15, 0, 0);
tableView.backgroundColor = [UIColor clearColor];

但是通过设置框架将表格视图的宽度增加15个像素。

答案 1 :(得分:3)

似乎我能找到的唯一解决方案是继承UITableViewCell,然后覆盖imageView和textLabel的布局。在研究这种方法时,我发现了this related answer。 (我想我一直都没有意识到我在寻找最初在iOS 6中的tableview行为。)

我的子类有这个唯一被覆盖的方法:

- (void)layoutSubviews
{
    [super layoutSubviews];

    // Slide image to the left, eliminating gutter
    self.imageView.frame = CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height);

    // Slide text label to the left, and extend width by additional
    // space gained by eliminating the left-most gutter
    self.textLabel.frame = CGRectMake((self.imageView.frame.origin.x + self.imageView.frame.size.width + 15), 0, self.textLabel.frame.size.width + 15, self.textLabel.frame.size.height);
}

我生成的tableview现在看起来完全符合我的要求:

enter image description here

答案 2 :(得分:1)

我想我刚刚提出了一个简单的解决方案,我在stackoverflow上找到了,一种调整单元格边距的方法,在代码中添加此方法

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

这解决了我的问题。

答案 3 :(得分:1)

1。在iOS 13上

关键是要与其他内容一起设置 cell.contentView.layoutMargins.zero

/// view controller
...
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "identifier")
tableView.separatorInset = .zero
tableView.directionalLayoutMargins = .zero
tableView.layoutMargins = .zero
...

/// data source
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "identifier")!
    cell.textLabel?.text = "Text"
    cell.directionalLayoutMargins = .zero
    cell.layoutMargins = .zero
    cell.contentView.directionalLayoutMargins = .zero
    cell.contentView.layoutMargins = .zero
    return cell
}

如果您正在使用UITableViewController,则除了上述所有方法外,还需要应用以下内容:

tableViewController.viewRespectsSystemMinimumLayoutMargins = false

2。在iOS 12及更低版本上

似乎没有办法使用layoutMargins等将标准UITableViewCell.textLabel相对于表的偏移量设置为零。我发现最简单,唯一的方法是对单元格进行子类化,并像这样覆盖layoutSubviews

class MyCell: UITableViewCell {
    ...
    override func layoutSubviews() {
        super.layoutSubviews()
        let leading = contentView.directionalLayoutMargins.leading
        textLabel?.frame.origin.x = leading
    }
    ...
}

当然,您需要正确设置布局边距才能正常工作

答案 4 :(得分:0)

iOS 8+

在表格视图中:

table.separatorInset = UIEdgeInsets.zero     // or UIEdgeInsetsZero

您可能还需要:

table.layoutMargins = UIEdgeInsets.zero

贷记到https://stackoverflow.com/a/30640595/385273

答案 5 :(得分:0)

您可以尝试;

[cell.textLabel setTranslatesAutoresizingMaskIntoConstraints:false];
[cell.textLabel.centerYAnchor constraintEqualToAnchor:cell.contentView.centerYAnchor].active = YES;
[cell.textLabel.leadingAnchor constraintEqualToAnchor:cell.contentView.leadingAnchor constant:22].active = YES;