简体   繁体   中英

AutoLayout exceptions iOS6

I am currently using an xib with autolayout to create custom cells in collection view. I have different cell sizes for both orientation. Hence I change the width and height of the cell according to the orientation.

The problem is that when I change the device orientation I get huge number of warnings / exceptions in iOS6.

However when I do the same in iOS7 no warnings are there.

Though it's not affecting my functionality I would like to remove all these warnings.

Here is my XIB file

And following are the errors / warnings /exceptions I am getting.

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) 
(
"<NSLayoutConstraint:0x164371f0 V:[UILabel:0x16437240(21)]>",
"<NSLayoutConstraint:0x16438050 V:[UILabel:0x16437240]-(1)-[UILabel:0x16437670]>",
"<NSLayoutConstraint:0x16437fd0 V:[UILabel:0x16437670]-(5)-|   (Names: '|':LibraryCollectionViewCell:0x16436ae0 )>",
"<NSLayoutConstraint:0x16437f50 V:[UILabel:0x16436e10]-(0)-[UILabel:0x16437240]>",
"<NSLayoutConstraint:0x16437e90 V:|-(7)-[UILabel:0x16436e10]   (Names: '|':LibraryCollectionViewCell:0x16436ae0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x16354d00 h=--& v=--& V:[UICollectionView:0xb24f200(264)]>",
"<NSAutoresizingMaskLayoutConstraint:0x163513b0 h=-&- v=-&- LibraryCollectionViewCell:0x16436ae0.height == UICollectionView:0xb24f200.height - 264>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x164371f0 V:[UILabel:0x16437240(21)]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

I have tried [cell setTranslatesAutoresizingMaskIntoConstraints:NO] but it crashes the app instead.

Can anyone please provide some solution on this?

"<NSLayoutConstraint:0x164371f0 V:[UILabel:0x16437240(21)]>",
"<NSLayoutConstraint:0x16438050 V:[UILabel:0x16437240]-(1)-[UILabel:0x16437670]>",
"<NSLayoutConstraint:0x16437fd0 V:[UILabel:0x16437670]-(5)-|   (Names: '|':LibraryCollectionViewCell:0x16436ae0 )>",
"<NSLayoutConstraint:0x16437f50 V:[UILabel:0x16436e10]-(0)-[UILabel:0x16437240]>",
"<NSLayoutConstraint:0x16437e90 V:|-(7)-[UILabel:0x16436e10]   (Names: '|':LibraryCollectionViewCell:0x16436ae0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x16354d00 h=--& v=--& V:[UICollectionView:0xb24f200(264)]>",
"<NSAutoresizingMaskLayoutConstraint:0x163513b0 h=-&- v=-&- LibraryCollectionViewCell:0x16436ae0.height == UICollectionView:0xb24f200.height - 264>"

OK, from your constraints you have...

(Numbers in brackets are last 3 digits of reference).

Label (240) with height constraint fixed to 21.
Label (240) with a vertical gap of 1 to label (670).
Label (670) with a vertical gap of 5 to the superview (ae0) bottom.
Label (e10) with a vertical gap of 7 to the superview (ae0) top.

UICollectionView (200) with fixed height of 264.
LibraryCollectionViewCell (ae0) with an equal height to the collection view - 264 (200).

< Adam Savage >OK! There's your problem!< / Adam Savage >

The last constraint in the list will give the cell a height of 0. (264 - 264 = 0) However, in the cell you have a label with fixed height 21 and fixed vertical gaps of 7, 5 and 1.

264 - 264 = 0
21 + 7 + 5 + 1 = 36

36 != 0

You can fix this a number of ways. Remove the constraint between the cell and the collection view. Remove the fixed height constraint on the label. etc...

I'd recommend drawing out how you want it to work (on paper) and scribbling in the constraints that you need. Or put a picture of what you are trying to do into your question so we can try to help.

A lot has changed in the way Auto layout takes place from iOS 6.0 to iOS 7.0 .

This is quite evident from auto layout implementation via X-Code 4.x & X-Code 5.x .

Since Apple is now expecting apps to be built in X-Code 5.x, I would suggest to go with the same.

Check for the base SDK as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM