I've stumbled upon a strange behaviour, where cells added to a UITableView
were sometimes indented to the left. This only happens on iOS 8.3, and I can't find a clear pattern as to when this happens.
Anyone experiencing same thing?
My tableViewCells were seeing increased left and right margins when run on iOS 8.3 that I did not see on previous versions.
Setting:
self.contentView.preservesSuperviewLayoutMargins = NO;
fixed my problem and kept my margins consistent on all versions but be aware that this in only available on iOS 8+.
So here, for example, is where you might do this:
- (void)awakeFromNib {
if ([self.contentView respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
self.contentView.preservesSuperviewLayoutMargins = NO;
}
}
Well - this is strange...
It seems that sometimes UITableViewCellContentView
's contentView
is not aligned with the UITableViewCellContentView
itself. This happens when the UITableView
is in itself a part of a layout wider than the screen (as in the case of a horizontal pager).
Luckily, the solution is simple: add a constraint to align the contentView
with its parent. This can only be done programatically, as you can not edit the contentView
's constraints in the layout editor.
- (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]];
}
This only started happening on iOS 8.3, but the fix seems to be safe and backward compatible to earlier versions as well.
As always - please comment with your own experience.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With