title: UIImageViews embed in UIScrollView in code w/ auto layout on
body: <p>I am currently having trouble with my iOS app and Autolayout. I want to let the user scroll photos from Tumblr in a <code>UIScrollView</code> (similar to Photos app, when scrolling your library). So when my ImageViewController gets on screen, it has an array of posts but no data for the images yet. My problem is the following :</p>
  
  <p>In my scrollView, I want to add as many UIImageViewS as needed, and I would like them to be all the same size. How should I do it? I tried many (probably bad designed :-/) ways, like <code>initwithframe:</code> with auto layout on and keeping a reference to them in a <code>NSMutableArray</code>... </p>
  
  <p>My goal now is to add them to my scrollView in <code>viewDidLoad</code> and have correct constraints set.</p>
  
  <p>Thanks for your help and sorry for my poor English </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>OK I solved my problem: I used a nice scrollView and set its constraints with auto layout in <code>viewWillAppear</code></p>
  
  <p>Here is the code for those interested (sorry for layout) : 
  - (void)viewWillAppear:(BOOL)animated
  {</p>
  
  <pre><code>[super viewWillAppear:animated];
  [self downloadPhotos];
  [self.scrollView setContentOffset:CGPointMake(self.selectedImageIndex * self.scrollView.frame.size.width, 0) animated:NO];
  
  // Add UIImageViewS to self.scrollView with constraints and so on...
  NSMutableDictionary *viewsDictionnary = [[NSMutableDictionary alloc] init];
  NSMutableString *imageViewsString = [[NSMutableString alloc] init];
  NSMutableArray *imageViews = [[NSMutableArray alloc] init];
  
  for (int i = 0; i &lt; self.fetchedResultsController.fetchedObjects.count; i++) {
      UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.scrollView.bounds];
      imageView.image = [UIImage imageNamed:@"placeholder_imageView"];
      imageView.contentMode = UIViewContentModeScaleAspectFit;
      imageView.translatesAutoresizingMaskIntoConstraints = NO;
      [self.scrollView addSubview:imageView];
  
      [imageViews addObject:imageView];
      [viewsDictionnary setObject:imageView forKey:[NSString stringWithFormat:@"imageView%d", i]];
      [imageViewsString appendString:[NSString stringWithFormat:@"[imageView%d]", i]];
      [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
      [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
      [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0]];
  }
  self.imageViews = imageViews;
  NSString *horizontal = [NSString stringWithFormat:@"H:|%@|", imageViewsString];
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:horizontal options:0 metrics:0 views:viewsDictionnary]];
  [self.scrollView setContentOffset:CGPointMake(self.selectedImageIndex * self.scrollView.frame.size.width, 0) animated:NO];
  </code></pre>
  
  <p>}</p>