3

I am using a UICollectionVewCell with custom layout in which height of the different cell varies from cell to cell. I have used approach described here https://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest

I have setup the cell in the storyboard and all the constraints seem to be happy with each other. When I run the code, I get cell layout exactly what I want. No problem there.

Problem is that in the log I see a NSAutoresizingMaskLayoutConstraint that seems to be in conflict with the constraints that I have put myself. The NSAutoresizingMaskLayoutConstraint is something the xcode is adding itself. This constraint is being automatically put by xcode on the CollectionViewCell's own content view (this content view is visible only in the view hierarchy and not in the document outline).

2017-02-19 21:54:28.999546 TestProject[6612:395125] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 

( "", "", "", "", "", "", "" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.

View Hierarchy

I see quite a few other people have similar issues but following those posts and trying all the solutions listed there has not helped me in getting rid of this error/conflict in constraints. Some of the posts that I had tried following are Unable to simultaneously satisfy constraints, will attempt to recover by breaking constraint How to trap on UIViewAlertForUnsatisfiableConstraints?

One of the solutions that come almost close to fixing it but not completely is to change the priority of the constraint that to 999. But this adds small empty that doesn't look good.

Any suggestions of how I can remove this NSAutoresizingMaskLayoutConstraint warning without having to change the constraints that I have manually put?

Thanks.

Community
  • 1
  • 1
Neeraj
  • 93
  • 10

1 Answers1

3

I realize this was a while ago, but I'm mainly posting this comment for future people seeking help.

I was having some issues recently with a UICollectionViewCell subclass with dynamic height and NSAutoresizingMaskLayoutConstraint as well, and also perused the other links you mentioned without success.

In my case I had two subviews: subview A pinned to superview.top/left/right, and then pinned bottom to subview B, which was pinned superview.left/right/bottom. I had given explicit height values to A and B (height = 50 and height = 300, that sort of thing), and in that scenario I was having the same thing as you - an auto layout error about constraints that can't simultaneously be satisfied, but the layout itself ended up fine.

In the end, I removed the height constraint of subview A and instead laid it out differently that still let it be the proper height (I had a label within it, so I pinned the label's top/bottom spacing, rather than pinning the height of A explicitly). And the console error went away while I was still able to maintain the layout I needed. So I don't know why, but it seems like for me the error occurs when there are explicit heights all the way from top to bottom of the cell. Changing it up so that one or more of the views are pinned via spacing constraints rather than height constraints seems to play more nicely.

(Bonus follow-up for anyone else desperately searching in the future: I later tried to resize subview B by changing its height constraint constant, and that again led to the console error but a proper layout. In this case, then, changing the priority of subview B's height constraint to 999 allowed the error to go away.)

UberJason
  • 3,063
  • 2
  • 25
  • 50
  • Thanks a lot for the suggestion. I had to push the app to the appstore. Will try your fix in the next version. – Neeraj Jun 12 '17 at 02:07