MLQ
MLQ

Reputation: 13511

Autolayout rules relative to size classes and defined in Interface Builder are broken in UICollectionViewCell

I'm defining the Autolayout rules of my custom UICollectionViewCell in Interface Builder. I have two sets of rules:

I seem to have set these rules correctly because when I look at Preview in the Assistant Editor, the cell renders itself as expected, even when I rotate the device screen. However, when I run the app, the second set of layout rules (the ones for landscape mode) breaks when the simulator is on landscape mode. The console prints out that Autoresizing masks have been applied to some view and I need to disable it.

That doesn't make sense to me at all--I used Autolayout on the views in IB so the autoresizing masks should have been disabled. I've tried the following and I still can't get this to work:

Any thoughts?

Add: I just found out--the cell itself and its contentView's translatesAutoresizingMaskIntoConstraints property is true by default. I really don't think I should set these to false, though. I only have Autolayout rules to their subviews.

Upvotes: 1

Views: 450

Answers (2)

MLQ
MLQ

Reputation: 13511

Sorry--apparently, I messed up the constraints by mistake. It seems that setting the constraints for wAny hAny also messed up the constraints for the other size classes. I simply reapplied the constraints all over again.

To anyone who might stumble upon this question, some notes:

  • You do not have to set translatesAutoresizingMaskIntoConstraints for the collection view cell and its contentView to false. Simply apply Autolayout rules on the cell's subviews. (Actually, don't disable translatesAutoresizingMaskIntoConstraints for any view that you are not applying Autolayout to.)
  • Contrary to @pteofil's answer, UICollectionViewCells do in fact respond to size classes and trait collections (you can call self.traitCollection from inside a cell). You can have one set of Autolayout rules for one size class, and a different set for another.
  • If you have rules for all possible size classes, you don't have to set anything at all for wAny hAny. You may, however, add rules there that are shared by all the size classes that you're targeting, but you don't have to make that red error for insufficient constraints in IB go away.
  • You still need to set the collection view cell's size correctly in collectionView:layout:sizeForItemAtIndexPath:.
  • It seems that the only way to keep this kind of problem from happening is to set the constraints in IB very, very, very carefully.
  • As an additional tip, really look at the console's output where it enumerates the unsatisfiable constraints. Print the description of your views to get their addresses and to identify which views are being indicated. I personally realized that toying around with wAny hAny messed another size class's rules when the console printed a constraint that I did not intentionally add on the affected view.

Upvotes: 2

pteofil
pteofil

Reputation: 4163

UICollectionViewCell are not influenced by the device orientation. They keep their size and only the CollectionView that display them change size with rotation so you can fit 1 or 2 cells on a row.

You have to implement UICollectionViewDelegateFlowLayout and return different size for cell based on the device orientation.

- (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath

Upvotes: 0

Related Questions