title: UIScrollView Zoom Does Not Work With Autolayout
body: <p>Zooming with UIScrollView using a strictly autolayout environment does not seem to work.</p>
  
  <p>This is especially frustrating because the iOS 6 release notes certainly lead me to believe it should when the wrote about a "Pure Auto Layout approach" here <a href="http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html" rel="nofollow noreferrer">http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html</a></p>
  
  <p>I looked the the WWDC 2012 slides for sessions 202, 228, and 232 and didn't see an answer for this.</p>
  
  <p>The only question I've seen on the internet specifically for this issue is <a href="https://stackoverflow.com/questions/14026807/uiscrollview-zooming-with-auto-layout">UIScrollView zooming with Auto Layout</a>, but it doesn't provide code of the problem and there is no answer.</p>
  
  <p>This user <a href="https://stackoverflow.com/users/341994/matt">https://stackoverflow.com/users/341994/matt</a> has given many great responses to UIScrollView autolayout questions and even linked to code on git hub, but I haven't been able to find anything that answers this issue there.</p>
  
  <p>I have attempted to boil this issue down to the absolute minimum to make it clear.</p>
  
  <p>I created a new single view application with a storyboard, and made no changes in the interface builder.</p>
  
  <p>I added a large picture file to the project "pic.jpg".</p>
  
  <p>SVFViewController.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt; 
  @interface SVFViewController : UIViewController &lt;UIScrollViewDelegate&gt;
  @property (nonatomic) UIImageView *imageViewPointer;
  @end
  </code></pre>
  
  <p>SVFViewController.m</p>
  
  <pre><code>#import "SVFViewController.h"
  
  @interface SVFViewController ()
  
  @end
  
  @implementation SVFViewController
  
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
  
      UIScrollView *scrollView = [[UIScrollView alloc] init];
      UIImageView *imageView = [[UIImageView alloc] init];
      [imageView setImage:[UIImage imageNamed:@"pic.jpg"]];
      [self.view addSubview:scrollView];
      [scrollView addSubview:imageView];
  
      scrollView.translatesAutoresizingMaskIntoConstraints = NO;
      imageView.translatesAutoresizingMaskIntoConstraints = NO;
      self.imageViewPointer = imageView;
      scrollView.maximumZoomScale = 2;
      scrollView.minimumZoomScale = .5;
      scrollView.delegate = self;
  
      NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(scrollView,imageView);
      NSLog(@"Current views dictionary: %@", viewsDictionary);
      [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
      [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
      [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView]|" options:0 metrics: 0 views:viewsDictionary]];
      [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView]|" options:0 metrics: 0 views:viewsDictionary]];
  }
  
  -(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView{
      return self.imageViewPointer;
  }
  
  @end
  </code></pre>
  
  <p>Notice I made a particular effort to make this as much like the sample code provided in the iOS 6 release notes, just doing the bare minimum to implement zooming.</p>
  
  <p>So, the problem?</p>
  
  <p>When you run this application and pan around in the scroll view, everything is good. But when you zoom the problem is obvious, the image flickers back and forth, and the placement of the image within the scroll view gets more wrong with every zoom.</p>
  
  <p>It looks like there is battle going on for the content offset of the imageView, it seems it is being set to different values by two different things with every "zoom". (an NSLog of the content offset property of the imageView appears to confirm this).</p>
  
  <p>What am I doing wrong here? Does anyone know how to property implement zooming within a UIScrollView in an purely autolayout environment. Is there an example of this anywhere out there?</p>
  
  <p>Please help.</p>