objective c - How to change frame of uiview is using autolayout in iOS? -
i added 1 view in xib, set autolayout. when run, want to change frame of view. know if using autolayout, need set translatesautoresizingmaskintoconstraints = yes
in code. have done that, create new frame view shows warning message in console:
@ least 1 of constraints in following list 1 don't want. try this: (1) @ each constraint , try figure out don't expect; (2) find code added unwanted constraint or constraints , fix it. (note: if you're seeing nsautoresizingmasklayoutconstraints don't understand, refer documentation uiview property translatesautoresizingmaskintoconstraints) ( "<nsibprototypinglayoutconstraint:0x16e4b750 'ib auto generated @ build time view fixed frame' v:|-(0)-[uiview:0x16ed3c30] (names: '|':uiview:0x16ee06b0 )>", "<nsautoresizingmasklayoutconstraint:0x16e19e30 h=-&- v=-&- uiview:0x16ed3c30.midy == uiview:0x16ee06b0.midy + 44>", "<nsautoresizingmasklayoutconstraint:0x16e19e60 h=-&- v=-&- uiview:0x16ed3c30.height == uiview:0x16ee06b0.height>" ) attempt recover breaking constraint <nsibprototypinglayoutconstraint:0x16e4b750 'ib auto generated @ build time view fixed frame' v:|-(0)-[uiview:0x16ed3c30] (names: '|':uiview:0x16ee06b0 )> break on objc_exception_throw catch in debugger. methods in uiconstraintbasedlayoutdebugging category on uiview listed in <uikit/uiview.h> may helpful.
this code create new frame, want origin of y equal 0.
self.leftview.frame= cgrectmake(0, 0, 40, self.view.frame.size.height);
how can fix bug? please give advice. much.
if understand correctly, think need use stored nslayoutconstraint variable in class, update constraint's constant value 0 make uiview slide top.
also, when you're using autolayout, set:
myview.translateautoresizingmaskintoconstraint = no;
that's no instead of yes.
so example like:
@interface myviewcontroller() { nslayoutconstraint *myviewtopconstraint; } @end @implementation myviewcontroller -(void)setupmyconstraintsmethod { ... // --------------------------------------------------------------- // constraint here says "myview" top edge should pinned // top edge of view controller's view offset // of 50 pixels. // --------------------------------------------------------------- nslayoutconstraint *myviewtopconstraint = [nslayoutconstraint constraintwithitem:self.myview attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributetop multiplier:1.0 constant:50.0]; [self.view addconstraint:myviewtopconstraint]; } -(void)buttonpressed:(id)sender { // ------------------------------------------------------------ // want myview's top edge @ top of // view controller's view, set constant 0 // ------------------------------------------------------------ myviewtopconstraint.constant = 0; // animates sliding of "myview" [uiview animatewithduration:0.5 animations:^{ [self.view layoutifneeded]; }]; } @end
Comments
Post a Comment