swift - Dynamic Layout iOS with Collection View -
this might simple question -- i'm new ios development.
i have homepage collection view 4 cells take whole screen (top left, top right, bottom left, bottom right) in portrait mode. in landscape mode, want 4 cells side side in row.
is collection view right layout use? should use autoresizing this, or have use other layout constraints?
thank you!
uicollectionview right layout use. can use following making layout
func collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize { let flowlayout = collectionviewlayout as! uicollectionviewflowlayout let totalspace = flowlayout.sectioninset.left + flowlayout.sectioninset.right + (flowlayout.minimuminteritemspacing * cgfloat(4 - 1)) let size = int((collectionview.bounds.width - totalspace) / cgfloat(4)) return cgsize(width: size, height: size) }
following way can use inside view controller
class viewcontroller: uiviewcontroller, uicollectionviewdelegate, uicollectionviewdelegateflowlayout { @objc(collectionview:layout:sizeforitematindexpath:) func collectionview(_ collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitemat indexpath: indexpath) -> cgsize { let flowlayout = collectionviewlayout as! uicollectionviewflowlayout let totalspace = flowlayout.sectioninset.left + flowlayout.sectioninset.right + (flowlayout.minimuminteritemspacing * cgfloat(4 - 1)) let size = int((collectionview.bounds.width - totalspace) / cgfloat(4)) return cgsize(width: size, height: size) } }
Comments
Post a Comment