ios - Autolayout rules relative to size classes and defined in Interface Builder are broken in UICollectionViewCell -
i'm defining autolayout rules of custom uicollectionviewcell in interface builder. have 2 sets of rules:
- one iphones on portrait (wcompact, hregular). in size class, vertical-scrolling collection view shows 1 column of cells.
- another iphones on landscape , ipads (wregular, hany). in these size classes, collection view shows 2 columns of cells.
i seem have set these rules correctly because when @ preview in assistant editor, cell renders expected, when rotate device screen. however, when run app, second set of layout rules (the ones landscape mode) breaks when simulator on landscape mode. console prints out autoresizing masks have been applied view , need disable it.
that doesn't make sense me @ all--i used autolayout on views in ib autoresizing masks should have been disabled. i've tried following , still can't work:
did
self.contentview.settranslatesautoresizingmaskintoconstraints(false)
inawakefromnib()
. quiets console, layout still mess. this destroys layout portrait mode.for 2 size classes specified, ib throws no errors. does, however, wany hany. did not specify sufficient constraints size class because figured it's pointless--i have rules all possible combinations of size classes. anyway, tried adding placeholder constraints , checked "remove @ build time" in ib. ib error went away didn't fix error @ all.
any thoughts?
add: found out--the cell , contentview
's translatesautoresizingmaskintoconstraints
property true
default. don't think should set these false
, though. have autolayout rules subviews.
sorry--apparently, messed constraints mistake. seems setting constraints wany hany
messed constraints other size classes. reapplied constraints on again.
to might stumble upon question, notes:
- you do not have set
translatesautoresizingmaskintoconstraints
collection view cell ,contentview
false. apply autolayout rules on cell's subviews. (actually, don't disabletranslatesautoresizingmaskintoconstraints
view not applying autolayout to.) - contrary @pteofil's answer,
uicollectionviewcell
s in fact respond size classes , trait collections (you can callself.traitcollection
inside cell). can have 1 set of autolayout rules 1 size class, , different set another. - if have rules possible size classes, don't have set @
wany hany
. may, however, add rules there shared size classes you're targeting, don't have make red error insufficient constraints in ib go away. - you still need set collection view cell's size correctly in
collectionview:layout:sizeforitematindexpath:
. - it seems way keep kind of problem happening set constraints in ib very, very, carefully.
- as additional tip, @ console's output enumerates unsatisfiable constraints. print
description
of views addresses , identify views being indicated. realized toying aroundwany hany
messed size class's rules when console printed constraint did not intentionally add on affected view.