title: In iOS7, cannot drag ImageView out of the constraints of parent view to main body?
body: <p>In iOS, when dragging an <code>UIImageView</code> (red square) using <code>UIPanGestureRecognizer</code> in a <code>scrollview</code> (bar at bottom within another <code>UIImageView</code>), I cannot drag out of the constraints of the bar into the main body <code>UIView</code> sitting above? Any help would be greatly appreciated. Thanks.</p>
  
  <p>For clarity, here’s my view structure:</p>
  
  <pre><code>view (main controller)
    |  
    ——&gt; view (nav bar)
    |
    ——&gt; view (main body)
    |
    ——&gt; view (yellow bar)
                |——&gt; scroll view (dynamic)
                       |
                       ——&gt;view (dynamic - wider than scroll view)
                            |
                            ——&gt; image view (dynamically created red square)
  </code></pre>
  
  <p>which looks like:</p>
  
  <p><img src="https://i.stack.imgur.com/j1Os4.png" alt="enter image description here"></p>
  
  <p>what I'm doing:</p>
  
  <p>Basically, in <code>ViewController</code>.m, I’m programmatically creating a <code>scrollview</code> (<code>barScrollView</code>) and adding it to a view (yellow bar). I dynamically create another new view and pass it to a function and within that function, in a loop, I dynamically create several <code>ImageView</code> items (red squares), enable <code>userInteractionEnabled</code> on them, create the <code>UIPanGestureRecognizer</code> and add it to each <code>ImageView</code> created, then add them to the view that was passed in. Lastly, I add the view that was passed in to the <code>scrollview</code> </p>
  
  <p>my code:</p>
  
  <p>In storyboard:</p>
  
  <pre><code>- dragged in 3 UIViews (see image above)
  </code></pre>
  
  <p>In ViewController.h:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface ViewController : UIViewController
  @property (weak, nonatomic) IBOutlet UIView *navBar;
  @property (weak, nonatomic) IBOutlet UIView *mainBodyView;
  @property (weak, nonatomic) IBOutlet UIView *yellowBar;
  
  @end
  </code></pre>
  
  <p>In ViewController.m:</p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
      CGRect barRect = CGRectMake(0, 0, 320, 60);
      CGRect containerRect = CGRectMake(0, 0, 820, 60);
      UIScrollView *barSV = [[UIScrollView alloc] initWithFrame:barRect];     
      [_yellowBar addSubview:barSV];
      UIView *redSquaresContainerView = [[UIScrollView alloc] initWithFrame:containerRect];
      [self generateRedSquares:redSquaresContainerView]; 
      [barSV addSubview:redSquaresContainerView];
      barSV.contentSize =containerRect.size;
  }
  
  
  -(void)generateRedSquares:(UIView*) containerView{
      int x =0;
      for( int i = 0; i &lt; 5; i++ ) {
          CGRect redSquareRect = CGRectMake(x, 10, 30, 30);
          UIImageView* aRedSquareView = [[UIImageView alloc] initWithFrame:redSquareRect];
          aRedSquareView.backgroundColor = [UIColor redColor];    
          aRedSquareView.userInteractionEnabled = YES; 
          UIPanGestureRecognizer *dragRecognizer = [[UIPanGestureRecognizer      
            alloc]initWithTarget:self action:@selector(dragRedSquare:)];
          [aRedSquareView addGestureRecognizer:dragRecognizer];
          [containerView addSubview:aRedSquareView];
          x+=60;
      }
  }
  
  
  - (void) dragRedSquare:(UIPanGestureRecognizer*)recognizer{
      static CGRect originalFrame;
      if ([recognizer state] == UIGestureRecognizerStateBegan) {
          originalFrame = recognizer.view.frame;
      } else if ([recognizer state] == UIGestureRecognizerStateChanged) {
          // Drag moved
          CGPoint translate = [recognizer translationInView:recognizer.view];
          CGRect newFrame = CGRectMake(originalFrame.origin.x + translate.x,
                                   originalFrame.origin.y + translate.y,
                                   originalFrame.size.width,
                                   originalFrame.size.height);    
          if (CGRectContainsRect(recognizer.view.superview.bounds, newFrame)){
              recognizer.view.frame = newFrame;
          }
      } else if ([recognizer state] == UIGestureRecognizerStateEnded) {
          // Drag completed
      }    
  }
  </code></pre>