12

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?

Vaiden
  • 15,728
  • 7
  • 61
  • 91
  • I am experiencing the same thing.. http://stackoverflow.com/questions/29824830/has-anything-changed-with-custom-uitableview-prototype-cells-constraints-since-i – mm24 Apr 24 '15 at 08:34

5 Answers5

13

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;
    }
}
Mason Lee
  • 5,130
  • 1
  • 20
  • 15
medmonds
  • 131
  • 3
  • 1
    This worked perfectly for me. Just wrapped it in an `if(respondsToSelector)` check in `awakeFromNib` and it stopped the extra indentation. – Stonz2 Apr 27 '15 at 14:46
  • I'm experiencing the same thing! Could you tell me how to fix if I don't use storyboard? self.tableView = UITableView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height), style: UITableViewStyle.Plain) self.tableView!.delegate = self self.tableView!.dataSource = self self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") Thanks – webmastx Jun 07 '15 at 10:03
  • This helped with an issue I had when testing a tableView UI glitch in iOS 11 that wasn't there in iOS 10 as well. Thanks! – Bill Weakley Aug 09 '17 at 17:58
6

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.

Vaiden
  • 15,728
  • 7
  • 61
  • 91
  • 1
    Not sure how you discovered this, but I'm very grateful for it. Many of my table cells were looking weird and I had no idea why. This fixed all of them. Thanks! – Matt Long Apr 15 '15 at 15:44
  • Happy I could provide some help. – Vaiden Apr 15 '15 at 16:21
  • So I noticed that while this seems to have fixed this issue when I'm building using the 8.3 SDK and running on 8.2, when I actually upgrade an iPad to 8.3, it persists. Any other ideas? – Matt Long Apr 16 '15 at 03:37
  • This solution relies on adding a constraint to fix the glitch. If it still persists on your device then something else if removing or affecting said constraint. – Vaiden Apr 16 '15 at 09:41
  • @Vaiden thanks for the answer. Where shall I put that code? In the view controller containing the UITableView or somewhere else? – mm24 Apr 24 '15 at 08:37
  • I tried to add it to each cell awakeFromNib but it would not work.. any idea why? – mm24 Apr 24 '15 at 08:49
  • Try the solution from @medmonds instead – Mason Lee May 29 '15 at 18:42
  • tried in updateConstraints and awakeFromNib but not working. its crashing – user3279053 Jun 22 '15 at 18:37
4

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.

ilidar
  • 81
  • 5
0

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

Richard Shin
  • 663
  • 5
  • 17
  • This must be a different issue then. The issue that I (OP) reported was experienced by users who upgraded from iOS 8.2 to 8.3, using our app version compiled and submitted from XCode 6.2. – Vaiden Apr 22 '15 at 08:26
0

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

Daniyar
  • 2,975
  • 2
  • 26
  • 39