objective c - Adjust UICollectionViewFlowLayout When UICollectionView Frame Changes -
i have uicollectionview created programmatically, along uicollectionviewflowlayout. code looks like:
- (uicollectionview *)createcollectiontodisplaycontent:(nsarray *)array viewwithcellidentifier:(nsstring *)cellidentifier ofwidth:(cgfloat)width withheight:(cgfloat)height foremailview:(emailview *)emailview { cgfloat mininteritemspacing; if (!self.orientationislandscape) mininteritemspacing = 9.0f; else mininteritemspacing = 20.0f; uicollectionviewflowlayout *layout = [[uicollectionviewflowlayout alloc]init]; layout.itemsize = cgsizemake(220.0f, 45.0f); layout.sectioninset = uiedgeinsetsmake(5.0f, 0.0f, 5.0f, 0.0f); layout.minimumlinespacing = 10.0f; layout.minimuminteritemspacing = mininteritemspacing; //get pointer layout can change later emailview.personlabelslayout = layout; uicollectionview *collectionview = [[uicollectionview alloc]initwithframe:cgrectmake(40, 4, width, height) collectionviewlayout:layout]; collectionview.datasource = emailview; collectionview.delegate = emailview; collectionview.scrollenabled = no; collectionview.userinteractionenabled = yes; collectionview.backgroundcolor = [uicolor clearcolor]; [collectionview registerclass:[uicollectionviewcell class] forcellwithreuseidentifier:cellidentifier]; collectionview.autoresizingmask = (uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleheight | uiviewautoresizingflexiblerightmargin | uiviewautoresizingflexiblebottommargin); [collectionview reloaddata]; return collectionview; }
this code works fine display collection view. issue in trying change flow layout when device rotated. handle change in collection view frame using struts , springs (see above), , in willanimaterotationtointerfaceorientation method, adjust minimuminteritemspacing property of flow layout, , call reload data on collection view. here code that:
- (void)willanimaterotationtointerfaceorientation:(uiinterfaceorientation)tointerfaceorientation duration:(nstimeinterval)duration { cgfloat mininteritemspacing; if (tointerfaceorientation == uiinterfaceorientationlandscapeleft || tointerfaceorientation == uiinterfaceorientationlandscaperight) { mininteritemspacing = 20.0f; } else { mininteritemspacing = 9.0f; } self.visibleemailview = //...more code self.visibleemailview.personlabelslayout.minimuminteritemspacing = mininteritemspacing; [self.visibleemailview.personlabelscollectionview reloaddata]; //other stuff... }
ultimately, flow layout should adjust dramatically when view rotated: in terms of minimuminteritemspacing in terms of number of columns displays. however, layout not adjusting expect. having trouble figuring out happening. looks minimuminteritemspacing not being reset on rotation, , looks each cell, same cell being drawn on multiple times. also, i'm getting crashes say:
"the collection view's data source did not return valid cell -collectionview:cellforitematindexpath: index path <nsindexpath 0x9be47e0> 2 indexes [0, 6]"
in trying begin narrow down going on here, wondering if can tell me if approach correct, and/or if there obvious error in code might causing or of funky behavior.
are implementing this?
- (bool) shouldinvalidatelayoutforboundschange:(cgrect)newbounds { return yes; ]
Comments
Post a Comment