ios - Auto Layout: can't get the frame size of UICollectionViewCell subViews -
i have custom uicollectionviewcell subclass (mycell
), interface setup in interface builder using auto layout. cell has image view , label.
now, when configure cell, need know width , height of image view. sounds pretty simple, looks it's impossible.
in view controller:
- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { mycell *cell = [collectionview dequeuereusablecellwithreuseidentifier:@"mycell" forindexpath:indexpath]; cell.itemnumber = indexpath.item; return cell; }
in cell subclass using property's setter customize cell:
- (void)setitemnumber:(nsinteger)itemnumber { _itemnumber = itemnumber; self.label.text = [nsstring stringwithformat:@"%i", self.itemnumber]; // in actual project need know image view's width , hight request image // of right size server. sadly, frame {{0, 0}, {0, 0}} // (same bounds) nslog(@"%@", nsstringfromcgrect(self.myimageview.frame)); }
a full example project can found @ https://github.com/kevinrenskers/collectionviewautolayouttest.
so problem this: need know image view's size because need ask server generate image of correct size. , image view's size {0,0}..
i've tried customizing in -layoutsubviews
method too:
- (void)setitemnumber:(nsinteger)itemnumber { _itemnumber = itemnumber; } - (void)layoutsubviews { if (self.myimageview.frame.size.height) { self.label.text = [nsstring stringwithformat:@"%i", self.itemnumber]; nslog(@"%@", nsstringfromcgrect(self.myimageview.frame)); } }
sadly, more messed up. method called twice, first frame {{0, 0}, {0, 0}} , frame set correctly. if statement checks height. once start scrolling though, wrong labels showed wrong cells. don't understand what's going on here.
the problem makes more sense when try in example project.
setting width- , height constraints , making iboutlets sounds great option, sadly cells don't have fixed size , image needs shrink , grow cell. removing auto layout isn't option either.
in end added top, right, bottom , left spacing constraints on imageview (to superview), added iboutlets them , used this:
cgfloat height = self.contentview.bounds.size.height - self.topconstraint.constant - self.bottomconstraint.constant; cgfloat width = self.contentview.bounds.size.width - self.leftconstraint.constant - self.rightconstraint.constant;
Comments
Post a Comment