iOS 8.3 - UITableView单元格未对齐,左侧缩进

Vai*_*den 12 uitableview uiview ios ios8.3

我偶然发现了一个奇怪的行为,其中添加到a的单元格UITableView有时会向左缩进.这只发生在iOS 8.3上,我找不到关于何时发生这种情况的明确模式.

有没有人遇到过同样的事情?

小智 13

我的tableViewCells在iOS 8.3上运行时看到增加的左右边距,这是我之前版本中没有看到的.

设置:

    self.contentView.preservesSuperviewLayoutMargins = NO;
Run Code Online (Sandbox Code Playgroud)

解决了我的问题并在所有版本上保持我的边距一致,但请注意,这仅适用于iOS 8+.

所以在这里,例如,你可能会这样做:

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


Vai*_*den 6

嗯 - 这很奇怪......

看来,有时UITableViewCellContentViewcontentView不与对齐UITableViewCellContentView本身.当UITableView它本身是比屏幕宽的布局的一部分时(如在水平寻呼机的情况下),就会发生这种情况.

幸运的是,解决方案很简单:添加约束以使其contentView与父对齐.这只能以编程方式完成,因为您无法contentView在布局编辑器中编辑约束.

- (void)awakeFromNib {
    // Initialization code

    // iOS 8.3 bug, where contentView's x position isnt aligned with self's x position...
    // So we add a constraint to do the obvious...
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]];
}
Run Code Online (Sandbox Code Playgroud)

这仅在iOS 8.3上开始,但修复似乎也安全且向后兼容早期版本.

一如既往 - 请根据自己的经验评论.