Ricky
Ricky

Reputation: 3171

UICollectionViewCell layout margins never update

I have a UICollectionView which basically appears like a table, it has a horizontal stack of UICollectionViewCell views. I want the standard UIViewController layout margins (16pt on the left and right on an iPhone X) to apply to the contents of the cells. This happens in a UITableView.

For some reason my cells initialise with the out of the box margins (8pts) and never update.

Interface Builder

I have checked all these options in my cell's .xib, and can log out the layoutMargins property to reveal that the value is correct, but the subviews just never move.

Any ideas?

Upvotes: 0

Views: 619

Answers (1)

Ricky
Ricky

Reputation: 3171

It turns out that, although you can't see it in Interface Builder, a UICollectionViewCell has a content view, which does not by default inherit layout margins.

This solved it for me:

- (void)awakeFromNib
{
    [super awakeFromNib];

    self.contentView.preservesSuperviewLayoutMargins = YES;
    ...
}

Be careful on devices/views with safe area insets set, this may knock around the bottom insets of your cell.

Upvotes: 5

Related Questions