How to set content offset for collection view using NSLayoutConstraint

As you can see in the above video, the last row of the photos is getting cut by the buttons. Ideally you would need to have the collection view scroll up, so that the last row is correctly visible. This can be done by setting the content offset programmatically. Today I would be showing you another way to do it when using storyboards. First you need to set constraints in your storyboard between the superview and the container as shown below.
Screen Shot 2016-01-20 at 5.01.29 PM
Screen Shot 2016-01-20 at 5.01.20 PM
After setting this, lets move on to the code. You need to connect this view to constraint in the code.

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomTrailingSpaceConstraint;

Next in your logic you can increase or decrease the height of the constraint as show below.

BOOL isRunningOnPhone = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone);
CGFloat height = isRunningOnPhone ? 30.0f : 50.0f;
self.bottomTrailingSpaceConstraint.constant = height;

This will change your constraints automatically. You don’t need to worry about setting it programmatically as shown below.

Leave a Reply

Your email address will not be published.