Note that there are some explanatory texts on larger screens.

plurals
  1. POUIScrollView with dynamic number of views and Auto Layout iOS6
    text
    copied!<p>i am struggling with an UIScrollView in iOS6 using auto layout. What i am trying to do is setting up a scrollview holding a number of subviews (UIViews). Those subviews are created dynamically in a loop. Now i would like to add my contraints, so that it is working with auto layout.</p> <p>In viewDidLoad I got this code:</p> <pre><code>scrollView.pagingEnabled = YES; CGRect selfBounds = self.view.bounds; CGFloat width = CGRectGetWidth(self.view.bounds); CGFloat height = CGRectGetHeight(self.view.bounds); NSMutableString *constraintBuilding = [NSMutableString stringWithFormat:@"|"]; NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil]; for (int i = 0; i &lt; colors.count; i++) { CGFloat offset = width * i; UIView* view1 = [[UIView alloc] initWithFrame:CGRectOffset(selfBounds, offset, 0)]; [view1 setTranslatesAutoresizingMaskIntoConstraints:NO]; [view1 setBackgroundColor:[colors objectAtIndex:i]]; [scrollView addSubview:view1]; [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view1(height)]|" options:0 metrics:@{@"height":@(height)} views:NSDictionaryOfVariableBindings(view1)]]; [constraintBuilding appendString:[NSString stringWithFormat:@"[view%i(width)]",i+1]]; } NSMutableArray *views = [[NSMutableArray alloc] init]; for (int j = 0; j &lt; scrollView.subviews.count; j++) { if (![[scrollView.subviews objectAtIndex:j] isKindOfClass:[UIImageView class]]) { [views addObject:[scrollView.subviews objectAtIndex:j]]; } } [constraintBuilding appendString:@"|"]; [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraintBuilding options:0 metrics:@{@"width":@(width*colors.count)} views:NSDictionaryOfVariableBindings(views)]]; </code></pre> <p><strong>the looping works just fine, but i'll get an error on the last line</strong></p> <pre><code>[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraintBuilding options:0 metrics:@{@"width":@(width*colors.count)} views:NSDictionaryOfVariableBindings(views)]]; </code></pre> <p>constraintBuilding says " |[view1(width)][view2(width)][view3(width)]| " Actually i dont have a view2 and view 3 but i dont know how to set up the contraint?!</p> <p><strong>That is the exception:</strong></p> <pre><code>'NSInvalidArgumentException', reason: 'Unable to parse constraint format: </code></pre> <p>view1 is not a key in the views dictionary. |[view1(width)][view2(width)][view3(width)]| ^'</p> <p>For testing i have 3 objects in my colors array, but later this number will change dynamically.</p> <p><strong>Thanks for your help!</strong></p> <p>Chris</p> <p><strong>EDIT:</strong></p> <p>one more thing. I read <a href="https://stackoverflow.com/questions/15153407/my-uiscrollview-doesnt-work-with-auto-layout-in-ios6">this</a> article and it worked just fine. but unfortunately it is not in a loop with a dynamic number of views. :(</p> <p>AND</p> <p>I read <a href="https://developer.apple.com/library/ios/#technotes/tn2154/_index.html" rel="nofollow noreferrer">this</a> article from apple. but still i couldnt find my answer. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload