ios - How to add auto layout on UICollectionView programmatically? -


i create uicollectionview programmatically, , want add auto layout constraint on collection view can change frame later. following code not work.

i trying initialize collectionview this:

        self.rootcollectionview = [[uicollectionview alloc]initwithframe:cgrectmake(0, 100, self.bounds.size.width, self.bounds.size.height - 100) collectionviewlayout:[[uicollectionviewflowlayout alloc]init]];         self.rootcollectionview.datasource = self;         self.rootcollectionview.delegate = self;         self.rootcollectionview.backgroundcolor = [uicolor clearcolor];         [self addsubview:self.rootcollectionview];         [self.rootcollectionview registerclass:[caslideswitchviewcell class] forcellwithreuseidentifier:@"caslideswitchviewcell"];         self.topconstraint = 100;         float topcon = self.topconstraint;//self.topconstraint cgfloat property,         uicollectionview * cv = self.rootcollectionview;         cv.translatesautoresizingmaskintoconstraints = no;         nsarray * constraintarray = [nslayoutconstraint constraintswithvisualformat:@"h:|-0-[cv]-0-|" options:0 metrics:nil views:nsdictionaryofvariablebindings(cv)];         nsarray *constraintarray2 = [nslayoutconstraint constraintswithvisualformat:@"v:|-topcon-[cv]-0-|" options:0 metrics:@{@"topcon":@(topcon)} views:nsdictionaryofvariablebindings(cv)];         [self addconstraints:constraintarray];         [self addconstraints:constraintarray2]; 

then after receive data, trying change topconstraint based on data. this:

if (number <= 1) {     self.topscrollview.hidden = yes;     self.topconstraint = 0; }else{     self.topscrollview.hidden = no;     self.topconstraint = 100; } [self updateconstraints]; [self.rootcollectionview.collectionviewlayout invalidatelayout]; [self.rootcollectionview reloaddata]; 

however, top margin of uicollectionview 100.

do miss something? or not right add auto layout that?

the self.topconstraint float not nslayoutconstraint can not update constraint. can instead of using

nsarray *constraintarray2 = [nslayoutconstraint constraintswithvisualformat:@"v:|-topcon-[cv]-0-|" options:0 metrics:@{@"topcon":@(topcon)} views:nsdictionaryofvariablebindings(cv)]; 

you should use

nslayoutconstraint *toplayoutcontraint = [nslayoutconstraint constraintwithitem:cv                              attribute:nslayoutattributetop                              relatedby:nslayoutrelationequal                                 toitem:self                              attribute:nslayoutattributetop                             multiplier:1.0                               constant:100];  nsarray *constraintarray2 = @[                               toplayoutcontraint,                               [nslayoutconstraint constraintwithitem:cv                                                            attribute:nslayoutattributebottom                                                            relatedby:nslayoutrelationequal                                                               toitem:self                                                            attribute:nslayoutattributebottom                                                           multiplier:1.0                                                             constant:0]                               ]; 

then can

if (number <= 1) {     self.topscrollview.hidden = yes;     toplayoutcontraint.constant = 0; } else {     self.topscrollview.hidden = no;     toplayoutcontraint.constant = 100; }  [self setneedslayout]; [self.rootcollectionview reloaddata]; 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -