iosuitableviewuiviewios8.3

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?


Solution

  • 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.