iOS 8和layoutMargin中的自定义UITableViewCells

sim*_*mon 11 objective-c uitableview ios8

我的应用程序中有几个自定义UITableViewCells,主要由nibs定义.移动到iOS 8和Xcode 6时,左右边距不正确.这些单元格通常散布在具有默认单元格的表格视图中.

我做了一个示例项目,这是我正在谈论的保证金问题: 保证金问题

我能找到的唯一与此相关的是新物业layoutMargins.对于UITableViewCells,其值似乎会根据运行应用程序的设备而改变:

iPhone 6 and below - layoutMargin: {8, 16, 8, 16}
iPhone 6 Plus - layoutMargin: {8, 20, 8, 20}
Run Code Online (Sandbox Code Playgroud)

这似乎与我在标准单元格上看到的边缘对齐.但是,对于我的定制单元的内容是在细胞内contentView,它具有标准的UIView layoutMargin{8, 8, 8, 8}.这意味着绑定到Container Margin的任何自动布局约束都会添加不正确的间距.

我发现解决这个问题的唯一方法是添加以下内容 cellForRowAtIndexPath:

cell.contentView.layoutMargins = cell.layoutMargins;
Run Code Online (Sandbox Code Playgroud)

这似乎不是一个非常好的解决方案(特别是因为我需要将它包装在iOS8的支票中以保持兼容性).

有没有人有任何想法?我觉得我必须遗漏一些东西.

Wal*_*ers 14

您可能想要查看preservesSuperviewLayoutMargins属性.这听起来像你在寻找.

将以下内容添加到需要与标准单元格保持一致的任何单元格中:

- (void)layoutSubviews {
    [super layoutSubviews];

    if ([self.contentView respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        self.contentView.preservesSuperviewLayoutMargins = YES;
    }
}
Run Code Online (Sandbox Code Playgroud)


dam*_*ter 5

在您的UITableViewCell子类中,覆盖layoutMarginsDidChange并设置contentView的layoutMargins以匹配单元格的layoutMargins:

- (void)layoutMarginsDidChange {
    contentView.layoutMargins = layoutMargins
}
Run Code Online (Sandbox Code Playgroud)

我发现这是不是设置更可靠preservesSuperviewLayoutMarginsYES.