Note that there are some explanatory texts on larger screens.

plurals
  1. POAutolayout constraints: Unable to simultaneously satisfy constraints
    primarykey
    data
    text
    <p>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.</p> <p>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 </p> <pre><code>[self allBooksCollectionViewSetConstraints]; </code></pre> <p>I have set</p> <pre><code>[allBooksCollectionView setTranslatesAutoresizingMaskIntoConstraints:NO]; </code></pre> <p>This is my code</p> <pre><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}]]; } </code></pre> <p>I have tried removing the UICollectionView constraints from the UIScrollview, but it doesn't change a thing.</p> <pre><code>[scrollView removeConstraints:allBooksCollectionView.constraints]; </code></pre> <p>On orientation change I get the following error:</p> <pre><code>Unable to simultaniously satisfy constraints ... ( "&lt;NSLayoutConstraint:0x9aa49f0 V:[UICollectionView:0xc0b6000(849)]&gt;", "&lt;NSLayoutConstraint:0xb2b15d0 V:[UICollectionView:0xc0b6000(1049)]&gt;" ) Will attempt to recover by breaking constraint &lt;NSLayoutConstraint:0xb2b15d0 V:[UICollectionView:0xc0b6000(1049)]&gt; </code></pre> <p>But the other constraint needs to be broken! Not this one, because 1049 is cvHeight.</p> <p>How can I fix this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload