Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8.3 - UITableView cell not aligned, indentation to the left

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?

like image 322
Vaiden Avatar asked Apr 10 '15 23:04

Vaiden


2 Answers

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;
    }
}
like image 162
medmonds Avatar answered Nov 07 '22 07:11

medmonds


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.

like image 45
Vaiden Avatar answered Nov 07 '22 08:11

Vaiden