简体   繁体   English

iOS UITableView Cell缩进

[英]iOS UITableView Cell Indentation

I am setting up a UITableView programmatically. 我正在以编程方式设置UITableView。 I would like the content of the cell to span the entire width of the screen. 我希望单元格的内容跨越整个屏幕宽度。 I have successfully set the cell to span the width of the screen, but the content and separators are still inset significantly (iPad screenshot below). 我已成功将单元格设置为跨越屏幕宽度,但内容和分隔符仍然显着嵌入(下面的iPad屏幕截图)。

Here is the tableview layout setup in my view controller implementation: 以下是我的视图控制器实现中的tableview布局设置:

- (void) viewDidLoad {
    [super viewDidLoad];

    // table layout
    self.tableView.rowHeight = 192;
    UILayoutGuide *margins = [self.view layoutMarginsGuide];
    [self.tableView.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor] ;
    [self.tableView.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

    CGRect tableRect = self.view.frame;
    self.tableView.frame = tableRect;

    // table colors
    self.tableView.backgroundColor = [UIColor grayColor];
    self.tableView.separatorColor = [UIColor grayColor];
    UIView *backView = [[UIView alloc] init];
    [backView setBackgroundColor:[UIColor grayColor]];
    [self.tableView setBackgroundView:backView];
}

Then I set the cell's content: 然后我设置单元格的内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor blueColor];
    cell.indentationWidth = 0;
    cell.indentationLevel = 0;
    cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0);
    cell.contentView.backgroundColor = [UIColor purpleColor];
    return cell;
}

The cell background is blue, and it spans the width of the screen. 单元格背景为蓝色,横跨屏幕宽度。 The purple area in my screenshot is the contentView, and as you can see, it doesn't stretch to the right edge of the screen, and the cell text is inset at the left. 我的屏幕截图中的紫色区域是contentView,如您所见,它不会延伸到屏幕的右边缘,并且单元格文本位于左侧。 The separator is also inset at left and right. 分隔符也在左右两侧插入。

表截图

I discovered the issue thanks to the comment on my question by @technerd. 由于@technerd对我的问题的评论,我发现了这个问题。 Thank you! 谢谢!

I was testing my app on iOS 9.2, and I neglected to account for the new iOS9+ cell property cellLayoutMarginsFollowReadableWidth , which adjusts the cell layout by default. 我在iOS 9.2上测试我的应用程序,而忽略了新的iOS9 +单元格属性cellLayoutMarginsFollowReadableWidth ,它默认调整单元格布局。

To turn the auto-resizing off, you'll need to check the iOS version and then disable the property, as @technerd demonstrates: 要关闭自动调整大小,您需要检查iOS版本,然后禁用该属性,如@technerd演示:

Objective C 目标C.

- (void)viewDidLoad {
    [super viewDidLoad];

    //For iOS 9 and Above 

    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0) {
        self.tableView.cellLayoutMarginsFollowReadableWidth = NO;
    }
}

Swift 迅速

override func viewDidLoad() {
    super.viewDidLoad()

    //For iOS 9 and Above 
    if #available(iOS 9, *) {
        tableView.cellLayoutMarginsFollowReadableWidth = false
    }
}

Hope this helps others. 希望这有助于其他人。

The above solution doesn't really work perfectly anymore. 上述解决方案不再完全正常工作。 Although, all you need to do now is: 虽然,你现在需要做的就是:

tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

This will make your separator run throughout the width of the table view. 这将使您的分隔符在表视图的整个宽度上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM