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

*爱你&永不变心* 提交于 2019-12-03 07:26:33

问题


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?


回答1:


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;
    }
}



回答2:


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.




回答3:


Looks like it's because of layoutMargins property. I've unchecked my constraint second item "Relative to margin" and everything worked great. Here is the article.




回答4:


I've seen a variation of this issue with an app I'm working on. We're seeing some table view cells with incorrect leading/trailing alignment.

It's not connected to iOS 8.3, it only occurs when building with Xcode 6.3. I can take the same project and build it on Xcode 6.2 and it doesn't occur.

After some investigation, it turns out that this only occurs on the table view cells in xibs.

The workaround is to remove the leading/trailing constraint to margins and set them directly to the superview (i.e. the cell content view).




回答5:


This fixed for me. I've just stopped using UITableViewCell's imageView and textLabel properties reconnected to my outlets.



来源:https://stackoverflow.com/questions/29572043/ios-8-3-uitableview-cell-not-aligned-indentation-to-the-left

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!