ios - Setting initial height for constraint-animated UIView -
i want animate view (the green 1 in screenshots) it's bottom height defined proximity top layout guide. plan animate vertical distance constraint (top of green box top layout guide), so:
here's code:
- (ibaction)upbutton:(uibutton *)sender { self.animatedheightconstraint.constant = 20; [uiview animatewithduration:0.5 animations:^{ [self.view layoutifneeded]; } completion:nil]; }
fine, works.
however, scheme doesn't take account starting height of view, want small, zero. since @ point i'm relying on constant set in storyboard set starting height, constant can number, depending on device.
i need positive method set initial, device-independent starting point top of green uiview.
my first thought set height constraint, compiler detects conflict, , corrects breaking restraint.
my second thought, although seems pretty clumsy, set both constraints, remove vertical constraint @ runtime, remove height constraint , add vertical constraint.
am missing obvious, or there no more elegant way go this?
update:
since haven't received answer yet, tried implementing second thought, detailed above. added initialheightconstraint
green bar via code:
-(void)viewwillappear:(bool)animated { [self.view removeconstraint:self.animatedheightconstraint]; [self.greenbar addconstraint:self.initialheightconstraint]; self.initialheightconstraint.constant = 1; [self.view setneedslayout]; }
this works. greenbar
set desired height, different constant
in soryboard:
next, try swap out 2 constraints , animate animatedheightconstraint
:
- (ibaction)upbutton:(uibutton *)sender { [self.greenbar removeconstraint:self.initialheightconstraint]; [self.view addconstraint:self.animatedheightconstraint]; [self.view setneedslayout]; self.animatedheightconstraint.constant = 20; [uiview animatewithduration:0.5 animations:^{ [self.view layoutifneeded]; } completion:nil]; }
this doesn't work. crashes with:
terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'invalid parameter not satisfying: [constraint iskindofclass:[nslayoutconstraint class]]'
unfortunately, don't understand means. use help!
Comments
Post a Comment