ios - Resize UICollectionViewCell on rotation -
i've got uicollectionview takes whole of screen. uicollectionview
has cells big uicollectionview
itself, , utilize paging.
on rotation of device want cells adjust size new uicollectionview
size. accomplish interchanging layout object in willanimaterotationtointerfaceorientation:
:
- (void)willanimaterotationtointerfaceorientation:(uiinterfaceorientation)tointerfaceorientation duration:(nstimeinterval)duration { [super willanimaterotationtointerfaceorientation:tointerfaceorientation duration:duration]; [_collectionview setcollectionviewlayout:[self collectionviewflowlayoutfororientation:tointerfaceorientation] animated:yes]; }
with collectionviewflowlayoutfororientation:
beingness follows:
- (uicollectionviewflowlayout *)collectionviewflowlayoutfororientation:(uiinterfaceorientation)orientation { cgsize screensize = [[uiscreen mainscreen] bounds].size; cgfloat width = uiinterfaceorientationislandscape(orientation) ? max(screensize.width, screensize.height) : min(screensize.width, screensize.height); cgfloat height = uiinterfaceorientationislandscape(orientation) ? min(screensize.width, screensize.height) : max(screensize.width, screensize.height); uicollectionviewflowlayout *collectionviewflowlayout = [[uicollectionviewflowlayout alloc] init]; [collectionviewflowlayout setscrolldirection:uicollectionviewscrolldirectionhorizontal]; [collectionviewflowlayout setitemsize:cgsizemake(width, height)]; [collectionviewflowlayout setminimumlinespacing:0.0f]; [collectionviewflowlayout setsectioninset:uiedgeinsetszero]; [collectionviewflowlayout setminimuminteritemspacing:0.0f]; homecoming collectionviewflowlayout; }
it doesn't work intended.
first of all, message in console indicating doing not exclusively legitimate:
the behavior of uicollectionviewflowlayout not defined because: item height must less height of uicollectionview minus section insets top , bottom values.
it fair message, starting animation rotate, screen / uicollectionview
still has frame old orientation. i'd inclined abide it.
secondly, contentoffset
wrong after rotation doesn't recalculated.
i've seen other solutions invalidate layout in willrotatetointerfaceorientation:duration:
, doesn't recalculate contentoffset
either. followed changing contentoffset
, contentsize
follows, result not perfect either:
- (void)willrotatetointerfaceorientation:(uiinterfaceorientation)tointerfaceorientation duration:(nstimeinterval)duration { cgsize fromcollectionviewsize = [self collectionviewsizefororientation:[self interfaceorientation]]; cgsize tocollectionviewsize = [self collectionviewsizefororientation:tointerfaceorientation]; cgfloat currentpage = [_collectionview contentoffset].x / [_collectionview bounds].size.width; nsinteger itemcount = [_collectionview numberofitemsinsection:0]; uicollectionviewflowlayoutinvalidationcontext *invalidationcontext = [[uicollectionviewflowlayoutinvalidationcontext alloc] init]; [invalidationcontext setcontentsizeadjustment:cgsizemake((tocollectionviewsize.width - fromcollectionviewsize.width) * itemcount, tocollectionviewsize.height - fromcollectionviewsize.height)]; [invalidationcontext setcontentoffsetadjustment:cgpointmake(currentpage * tocollectionviewsize.width - [_collectionview contentoffset].x, 0)]; [[_collectionview collectionviewlayout] invalidatelayoutwithcontext:invalidationcontext]; [super willrotatetointerfaceorientation:tointerfaceorientation duration:duration]; }
so, question is: what solution gives intended result?
try adding line [_collectionview invalidateintrinsiccontentsize] before set layout. should cause re-calculate layout, and, rid of warning message.
ios rotation uicollectionview
No comments:
Post a Comment