ios - UICollectionViewFlowLayout with centered cells -


i'm trying implement horizontal aligned collectionview centered cells , i'm using custom uicollectionviewflowlayout. found several solutions uicollectionviewdelegateflowlayout delegate not called, if use custom uicollectionviewflowlayout.

here uicollectionviewflowlayout. ideas do?

class myflowlayout: uicollectionviewflowlayout {      var numberofrows = 1     var cellpadding: cgfloat = 15.0      var availablesize = cgsize.zero     fileprivate var cache = [uicollectionviewlayoutattributes]()     fileprivate var contentwidth: cgfloat  = 0.0     fileprivate var contentheight: cgfloat {          var collectionviewheight = collectionview!.bounds.height         if availablesize.height > 0{             collectionviewheight = availablesize.height         }          let insets = collectionview!.contentinset         return collectionviewheight - (insets.top + insets.bottom)     }      func reinit(_ newsize: cgsize){          availablesize = newsize         cache.removeall()     }       override func prepare() {          if cache.isempty {              let rowheight = contentheight / cgfloat(numberofrows)              var yoffset = [cgfloat]()             row in 0 ..< numberofrows {                 yoffset.append(cgfloat(row) * rowheight )             }             var row = 0             var xoffset = [cgfloat](repeating: 0, count: numberofrows)               item in 0 ..< collectionview!.numberofitems(insection: 0) {                  let indexpath = indexpath(item: item, section: 0)                  let height =  rowheight                 let width = cgfloat(height / 1.27)                  let frame = cgrect(x: xoffset[row], y: yoffset[row], width: width, height: height)                 let insetframe = frame.insetby(dx: cellpadding * 2, dy: cellpadding)                  let attributes = uicollectionviewlayoutattributes(forcellwith: indexpath)                 attributes.frame = insetframe                 cache.append(attributes)                  contentwidth = max(contentwidth, frame.maxx)                 xoffset[row] = xoffset[row] + width                  row = row >= (numberofrows - 1) ? 0 : (row + 1)             }         }     }       override var collectionviewcontentsize : cgsize {          return cgsize(width: contentwidth, height: contentheight)     }      override func layoutattributesforelements(in rect: cgrect) -> [uicollectionviewlayoutattributes]? {          var layoutattributes = [uicollectionviewlayoutattributes]()          attributes in cache {             if attributes.frame.intersects(rect) {                 layoutattributes.append(attributes)             }         }         return layoutattributes     } } 

the layout connected way:

collectionview.collectionviewlayout = myflowlayout() collectionview.register(mycell.self, forcellwithreuseidentifier: mycell.identifier) collectionview.datasource = self 

prepare in myflowlayout called.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -