Jakub Flejmer
Jakub Flejmer

Reputation: 11

UICollectionViewCell contentView padding

I want to set padding for contentView from inside of custom UICollectionViewCell so that it is smaller than the cell itself. I tried doing that by setting anchors for contentView but this view seems to always be equal size of the cell.

This is what I tried.

 self.contentView.translatesAutoresizingMaskIntoConstraints = false;
 [self.contentView.topAnchor constraintEqualToAnchor:self.topAnchor constant:5].active = true;
 [self.contentView.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:5].active = true;
 [self.contentView.rightAnchor constraintEqualToAnchor:self.rightAnchor constant:-5].active = true;
 [self.contentView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-5].active = true;

For a quick solution I created another view inside contentView that I can anchor the way shown above but I hope for a cleaner solution.

Upvotes: 1

Views: 2090

Answers (1)

user9469089
user9469089

Reputation:

You might want to try setting edge insets for the contentView, but if you take that approach you should be pinning the subviews to the layout margins guides and not anchors directly.

I come from Swift, but I think you'll understand me:

contentView.layoutMargins = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
someSubview.topAnchor.constraintEqualToAnchor(contentView.layoutMarginsGuide.topAnchor).isActive = true

Upvotes: 4

Related Questions