Friday, 15 August 2014

UIScrollView and Autolayout: ContentOffset is changing but content view is not moving -



UIScrollView and Autolayout: ContentOffset is changing but content view is not moving -

i have been trying implement autolayout on uiscrollview have been struggling. here description of main view, top bottom: navigation bar -> scroll view -> uitextfield, of stretch horizontally across screen. here constraints these 3 views:

//vertical constraints; appears working fine [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|-navheight-[_inputscrollview][_inputfield(inputfieldheight)]|" options:0 metrics:metricsdict views:viewsdict]]; //horizontal constraints; these appear working fine [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|[_inputscrollview]|" options:0 metrics:metricsdict views:viewsdict]]; [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|[_inputfield]|" options:0 metrics:metricsdict views:viewsdict]];

next, countless uiscrollview/autolayout tutorials suggest, added subview uiscrollview deed content view many other subviews, added dynamically @ runtime. when application launches there nil in content view. here constraints content view like:

//left constraint, pinning content view left border of screen nslayoutconstraint *leftconstraint = [nslayoutconstraint constraintwithitem:_contentview attribute:nslayoutattributeleading relatedby:0 toitem:self.view attribute:nslayoutattributeleading multiplier:1.0 constant:0]; [self.view addconstraint:leftconstraint]; //right constraint, pinning content view left border of screen nslayoutconstraint *rightconstraint = [nslayoutconstraint constraintwithitem:_contentview attribute:nslayoutattributetrailing relatedby:0 toitem:self.view attribute:nslayoutattributetrailing multiplier:1.0 constant:0]; [self.view addconstraint:rightconstraint]; //top constraint, setting top of content view //to offset top of main view //by height of navigation bar nslayoutconstraint *topconstraint = [nslayoutconstraint constraintwithitem:_contentview attribute:nslayoutattributetop relatedby:0 toitem:self.view attribute:nslayoutattributetop multiplier:1.0 constant:navheight.floatvalue]; [self.view addconstraint:topconstraint]; //bottom constraint, setting bottom of content view //to offset bottom of main view //height of text field. nslayoutconstraint *bottomconstraint = [nslayoutconstraint constraintwithitem:_contentview attribute:nslayoutattributebottom relatedby:0 toitem:self.view attribute:nslayoutattributebottom multiplier:1.0 constant:-inputfieldheight.floatvalue]; [self.view addconstraint:bottomconstraint];

after these views laid out, height of content view equal height of scroll view. have alwaysbouncevertical property set yes, expect see scrolling. when scroll on screen, content offset of scroll view change! implemented scrollviewdidscroll , logged contentoffset screen. however, content view not move @ all. set background color of scroll view reddish , background color of content view black. when contentoffset changing, still never see of reddish scrollview. content view subview of scrollview, why in world frame not changing?! help so so appreciated. thanks!

i had set constraints in next way:

1) tie content view scroll view adding top, bottom, left, , right constraints (e.g. top of content view = top of scroll view). 2) define explicit height , width constraints content view. in case, , in cases, constants these constraints should based off of height/width of content view's subviews.

once implemented design scrolling worked appropriately.

uiscrollview autolayout nslayoutconstraint contentoffset

No comments:

Post a Comment