1

I have an issue with adding a NSLayoutConstraint. I'd like to update the height of an UICollectionView so that all cells fit in the UICollectionView without scrolling. This is because I have put the UICollectionView in a UIScrollView, together with other UI Elements.

I have set the constraints in the interface builder, and I resize the UICollectionView on viewDidLoad, when I know how many items should be displayed. I do this with

[self allBooksCollectionViewSetConstraints];

I have set

[allBooksCollectionView setTranslatesAutoresizingMaskIntoConstraints:NO];

This is my code

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation {
    [self allBooksCollectionViewConstraints];
}

-(NSInteger)allBooksCollectionViewHeight
{
    float booksPerRow;
    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
    {
        booksPerRow = 6.0;
    }
    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
    {
        booksPerRow = 8.0;
    }
    //calculate right height do display all cells
    NSInteger cvHeight = (ceil((float)[allBooksCollectionView numberOfItemsInSection:0]/booksPerRow)*200.0)+49.0;
    return cvHeight;
}

-(void)allBooksCollectionViewSetConstraints
{
    NSInteger cvHeight = [self allBooksCollectionViewHeight];

    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[view(%d)]", cvHeight] options:0 metrics:nil views:@{@"view":allBooksCollectionView}]];
}

I have tried removing the UICollectionView constraints from the UIScrollview, but it doesn't change a thing.

[scrollView removeConstraints:allBooksCollectionView.constraints];

On orientation change I get the following error:

Unable to simultaniously satisfy constraints ... (
"<NSLayoutConstraint:0x9aa49f0 V:[UICollectionView:0xc0b6000(849)]>",
"<NSLayoutConstraint:0xb2b15d0 V:[UICollectionView:0xc0b6000(1049)]>"
)

Will attempt to recover by breaking constraint <NSLayoutConstraint:0xb2b15d0 V:[UICollectionView:0xc0b6000(1049)]>

But the other constraint needs to be broken! Not this one, because 1049 is cvHeight.

How can I fix this?

Mathijs
  • 1,258
  • 1
  • 10
  • 27

1 Answers1

1

I have tried removing the UICollectionView constraints from the UIScrollview, but it doesn't change a thing.

[scrollView removeConstraints:allBooksCollectionView.constraints];

This line of code is wrong. None of the constraints returned from collectionView.constraints will be on the scrollview, so this call will do nothing. You should store the constraints you care about in a property or instance variable:

if (collectionViewHeightConstraints)
{
    [scrollView removeConstraints:collectionViewHeightConstraints];
}
collectionViewHeightConstraints = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[view(%d)]", cvHeight] options:0 metrics:nil views:@{@"view":allBooksCollectionView}];
[scrollView addConstraints:collectionViewHeightConstraints];
jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Thanks, feel like this will get me somewhere. I have some troubles implementing your line with collectionViewHeightConstraints = [NSLayoutConstraint constraintsWithVisualFormat:...]; though. I get a warning about Incompatible pointer types assigning NSlayoutConstraint from NSArray. – Mathijs Dec 18 '13 at 12:51
  • The problem was like you said, the constraints were set on the UICollectionView, not on the UIScrollView. To remov them I use [allBooksCollectionView removeConstraints:allBooksCollectionView.constraints]; now and to set them I use [allBooksCollectionView addConstraints:[NSLayoutConstraint.... Thanks! – Mathijs Dec 18 '13 at 13:03
  • In my example code, collectionViewHeightConstraints is an NSArray. – jrturton Dec 18 '13 at 13:21
  • Shouldn't the syntax then be [collectionViewHeightConstraints addObject:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[view(%d)]", cvHeight] options:0 metrics:nil views:@{@"view":allBooksCollectionView}]; [scrollView addConstraints:collectionViewHeightConstraints]]; ? – Mathijs Dec 18 '13 at 13:27
  • No, constraintsWithVisualFormat returns an array. – jrturton Dec 18 '13 at 14:15