i'm trying set view auto layout animates nicely when view changes size...
the scenario is...
i have uiview
subview called animview
. animview
starts outside of frame of view. view grows (gets taller), animview
moves down @ same rate. when animview
10 points top of view (i.e. @"v:|-10-[animview]" ) stops , "sticks" point.
i.e. this...
this works (dropbox share project):
the trick use invisible uiview spacer object. view call animview set this. because set partially in ib, still raises few issues, see works want.
@implementation animview { uiview * _innerbox; uiview * _spacer; }
then call method - setupeverything
initwithcoder:
or initwithframe:
.
- (void) setupeverything { // because set in ib autolayout off, think // still needs this. won't work without self.translatesautoresizingmaskintoconstraints = yes; // otherwise see box outside of animview self.clipstobounds = yes; _innerbox = [[uiview alloc] init]; _innerbox.backgroundcolor = [uicolor blackcolor]; _spacer = [[uiview alloc] init]; _spacer.backgroundcolor = [uicolor clearcolor]; _innerbox.translatesautoresizingmaskintoconstraints = no; _spacer.translatesautoresizingmaskintoconstraints = no; [self addsubview:_innerbox]; [self addsubview:_spacer]; // give _innerbox , _spacer dimensions prevent ambiguity nslayoutconstraint *i02 = [nslayoutconstraint constraintwithitem:_innerbox attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:nil attribute:0 multiplier:1.0f constant:50.0f]; nslayoutconstraint *i03 = [nslayoutconstraint constraintwithitem:_innerbox attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:nil attribute:0 multiplier:1.0f constant:50.0f]; nslayoutconstraint *s02 = [nslayoutconstraint constraintwithitem:_spacer attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:nil attribute:0 multiplier:1.0f constant:50.0f]; // center both views nslayoutconstraint *i05 = [nslayoutconstraint constraintwithitem:_innerbox attribute:nslayoutattributecenterx relatedby:nslayoutrelationequal toitem:self attribute:nslayoutattributecenterx multiplier:1.0f constant:0.0f]; nslayoutconstraint *s04 = [nslayoutconstraint constraintwithitem:_spacer attribute:nslayoutattributecenterx relatedby:nslayoutrelationequal toitem:self attribute:nslayoutattributecenterx multiplier:1.0f constant:0.0f]; // set top of _innerbox 10.0 points top of superview low priority nslayoutconstraint *i01 = [nslayoutconstraint constraintwithitem:_innerbox attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:self attribute:nslayoutattributetop multiplier:1.0f constant:10.0f]; i01.priority = 250; // pin spacer bottom of _innerbox; nslayoutconstraint *i04 = [nslayoutconstraint constraintwithitem:_innerbox attribute:nslayoutattributebottom relatedby:nslayoutrelationequal toitem:_spacer attribute:nslayoutattributetop multiplier:1.0f constant:0.0f]; // pin spacer's bottom bottom of superview nslayoutconstraint *s01 = [nslayoutconstraint constraintwithitem:_spacer attribute:nslayoutattributebottom relatedby:nslayoutrelationequal toitem:self attribute:nslayoutattributebottom multiplier:1.0f constant:0.0f]; // stretch spacer out needed nslayoutconstraint *s03 = [nslayoutconstraint constraintwithitem:_spacer attribute:nslayoutattributeheight relatedby:nslayoutrelationgreaterthanorequal toitem:nil attribute:0 multiplier:1.0f constant:80.0f]; [self addconstraints:@[i01,i02,i03,i04,i05,s01,s02,s03,s04]]; }
Comments
Post a Comment