ios - How to set dynamic UICollectionView cell height? -
how set dynamic cell height?
i need set cell size in method:
func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell
i try this:
cell.bounds = cgrect(x: cell.bounds.origin.x, y: cell.bounds.origin.y, width: width, height: height)
but can't scrool down...
and try methiod:
func collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize
but method ben call first, call method:
cell.bounds = cgrect(x: cell.bounds.origin.x, y: cell.bounds.origin.y, width: width, height: height)
so.. how change cell height?
firstly, set per normal. (normal constraints [left,right,top,bottom, , height] , iboutlet uicollectionview.
secondly, make iboutlets height constraint, top constraint, , bottom constraint.
thirdly, add ->
//these outlets naming generic names name them wish iboutlet var collectionviewcelldynamicconstraint: nslayoutconstraint! iboutlet var topconstraintdynamicconstraint: nslayoutconstraint! iboutlet var bottomconstraintdynamicconstraint: nslayoutconstraint! //into viewdidload collectionviewcell.addobserver(self, forkeypath: "contentheight", options:nskeyvalueobservingoptions.new, context: nil) topconstraintdynamicconstraint.addobserver(self, forkeypath: "contenttop", options:nskeyvalueobservingoptions.new, context: nil) bottomconstraintdynamicconstraint.addobserver(self, forkeypath: "contentbottom", options:nskeyvalueobservingoptions.new, context: nil) //create override func override func observevalueforkeypath(keypath: string?, ofobject object: anyobject?, change: [string : anyobject]?, context: unsafemutablepointer<void>) { //try use segment below if doesn't work remove , try run again self.collectionviewcell.sizethatfits() // self.collectionviewcelldynamicconstraint.constant = collectionviewcell.sizethatfits(cgsizemake(self.collectionviewcell.frame.size.width, cgfloat.max)).height // set these values (below) space/value set them @ normally. needed refresh after changing height of view/collectionviewcell self.topspaceold.constant = 0.4 self.bottomspaceold.constant = 10 } // need de-initialise them before leave view or else crash deinit { self.collectionviewcell.removeobserver(self, forkeypath: "contentheight") self.topconstraintdynamicconstraint.removeobserver(self, forkeypath: "contenttop" self.bottomconstraintdynamicconstraint.removeobserver(self, forkeypath: "contentbottom") ) }
there might naming issues should work. code worked getting uitextview work inside of scroll view dynamic height of view. should same concept.
Comments
Post a Comment