Monday 15 April 2013

ios - UICollectionViewCell dynamic sizing for 4.4 and 5.5 inch -



ios - UICollectionViewCell dynamic sizing for 4.4 and 5.5 inch -

i have uicollectionview cells in storyboard. size of each cell set 145x145.

they on iphone 4 - 5s, size doesn't increment proportionally on iphone 6 , 6+. rather manually setting different cell size each device, how can dynamically?

see examples here: iphone 5s: http://prntscr.com/55vy7q, iphone 6: http://prntscr.com/55vyy2

if want cells adjust width, you'll need calculate flow layout's itemsize based on view width.

if cells same size, can set flow layout's itemsize property:

#define kcellsperrow 2 uicollectionviewflowlayout *flowlayout = (uicollectionviewflowlayout*)self.collectionview.collectionviewlayout; cgfloat availablewidthforcells = cgrectgetwidth(self.collectionview.frame) - flowlayout.sectioninset.left - flowlayout.sectioninset.right - flowlayout.minimuminteritemspacing * (kcellsperrow - 1); cgfloat cellwidth = availablewidthforcells / kcellsperrow; flowlayout.itemsize = cgsizemake(cellwidth, flowlayout.itemsize.height);

if need calculate per-cell sizes, can in collectionview:layout:sizeforitematindexpath: delegate.

recalculate itemsize when device orientation changes , update layout:

redraw layout without animation:

[self.collectionview.collectionviewlayout invalidatelayout]

animate layout changes:

[self.collectionview performbatchupdates:nil completion:nil]

ios objective-c iphone autolayout uicollectionviewcell

No comments:

Post a Comment