Reputation: 13511
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:
Did self.contentView.setTranslatesAutoresizingMaskIntoConstraints(false)
in awakeFromNib()
. This only quiets the console, but the layout is still a mess. This also destroys the layout for portrait mode.
For the two size classes I specified, IB throws no errors. It does, however, for wAny hAny. I did not specify sufficient constraints for that size class because I figured it's pointless--I have rules for all possible combinations of the size classes. Anyway, I tried adding placeholder constraints and checked "Remove at build time" in IB. The IB error went away but it didn't fix the error at all.
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
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:
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.)UICollectionViewCell
s 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.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.collectionView:layout:sizeForItemAtIndexPath:
.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
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