0
votes

I am having an error in my iPad project with auto layout constraints. It's happening with a custom UICollectionViewCell I've created in IB, when I rotate the device to landscape. The thing is that none of the constraints are deletable. I'm still learning auto layout, so I'm sure it could just be a matter of adding some restraints and deleting some of the default ones, but I'm stuck. It's definitely happening with my custom cell - when I remove it and use a plain old UICollectionViewCell I get no error.

I searched around and I tried setting translatesAutoresizingMaskIntoConstraints on my custom cell to NO but then I get the following error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UICollectionView's implementation of -layoutSubviews needs to call super.'

Works fine to set it to NO on the subviews of my custom cell, but when I set it on the cell itself it barfs.

Here is the auto layout constraint error I'm getting - it's the usual one:

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)

(
"NSAutoresizingMaskLayoutConstraint:0x76bf950 h=-&- v=-&- CMAGalleryCollectionViewCell:0x7687690.height == UICollectionView:0x9161e00.height - 875",

"NSAutoresizingMaskLayoutConstraint:0x76693c0 h=--& v=--& V:[UIView:0x7665470(704)]",

"NSLayoutConstraint:0x7665850 UICollectionView:0x9161e00.bottom == UIView:0x7665470.bottom",

"NSLayoutConstraint:0x7665740 V:|-(0)-[UICollectionView:0x9161e00] (Names: '|':UIView:0x7665470 )" )

Will attempt to recover by breaking constraint NSLayoutConstraint:0x7665850 UICollectionView:0x9161e00.bottom == UIView:0x7665470.bottom

Any help or suggestions greatly appreciated.

Thanks!

1
Please include the list of constraints from the error log as welljrturton
Sorry for the dumb question but where exactly would I find the error log?fogwolf
Right underneath the error you quote above. It will be a list of constraints inside angle brackets.jrturton
Oh right. Added to original message. Thanks.fogwolf

1 Answers

0
votes

I've hit this a lot. When you rotate the device/application the width/height of the view change to fit the new orientation screen, and that often breaks a "width = 320" type constraint.

I found the exception above to be unreadable until I'd read this: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/formatLanguage.html

... which is similar to the format shown, and then it became quite easy to see what was a problem.

The non-deletable constraints you are seeing can be altered. You can lower the priority from 1000 to something that can be broken or you can replace them with something that works. One (imperfect) approach might be to make a rule >= 320 so things could stretch for landscape, or you could add your own constraints to give (perhaps tying the view to it's superview, and then the width constraint should be deletable.) That said, that is not a good solution.

I'd recommend you typically attempt to remove width and height constraints where at all possible on something (like a large collection view) that should react to the shape/size of the main view 'cos those widths/heights will typically be wrong 50% of the orientations.

That all said, is this saying the cell height is collection height minus 875?

CMAGalleryCollectionViewCell:0x7687690.height == UICollectionView:0x9161e00.height - 875",

Is this cell a sub view of the collection view and not a template for cells? I've had that problem with interface builder and collections also. I had to rebuild the collection view from scratch in IB to not add a bogus child cell.