ios - UICollectionviewCell: compute perfect cellsize -


i'm having uicollectionview gets populated different numbers of cells. cells should use available space perfect possible. compute available space per cell.

the available space should computed when view initializes. each cell has spacing of 2pt everything. have hard code space of navigationbar , statusbar 64pt, not future proof.

with code below working in simulator, not on real hardware (iphone5s): on physical iphone height not computed correctly , and gap 30pt appears between last row , bottom of collection view.

any idea how improve code?

private func computecellsizes(var size :cgsize){          if(size == cgsizezero){             size = self.collectionview!.frame.size         }          self.numberofrows  = int(size.width / 103)          //spacing of 2 points between each cell         let availablewidth = size.width - cgfloat((self.numberofrows + 1)  * 2)         let cellwidth = availablewidth / cgfloat(numberofrows)          self.numberofcolumns = int(size.height / (cellwidth * 1.3))         //spacing of 2 points between each cell, minus navigationcontroller height         let availableheight = size.height - cgfloat((numberofcolumns + 1)  * 2) - 64         let cellheight = availableheight / cgfloat(numberofcolumns)          let cellsize =  cgsizemake(cellwidth, cellheight)          self.cachedsizes.setobject(nsvalue(cgsize: cellsize), forkey: size.width)     } 

i (in uicollectionviewcontroller implementation):

- (uiedgeinsets)collectionview:(uicollectionview *)collectionview layout:(uicollectionviewlayout*)collectionviewlayout insetforsectionatindex:(nsinteger)section {     return uiedgeinsetsmake(10, 10, 10, 10); //spacing on margins (top, left, bottom, right) }  - (cgsize)collectionview:(uicollectionview *)collectionview                   layout:(uicollectionviewlayout *)collectionviewlayout   sizeforitematindexpath:(nsindexpath *)indexpath {     return cgsizemake((self.view.frame.size.width - 40)/3, 100.0); // show 3 cell 1 row (-40 - spaces between cells , screen sides) } 

Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -