title: Move entire UIScrollView when scrolling. When at top, start scrolling normally
body: <p>I have a scrollview, or actually a UIWebView, which is initially positioned 320px from the top of its parent view.</p>
  
  <p>When the user starts scrolling, I want the entire UIWebView to follow the movement upwards until it reaches the top, then the webview should start scrolling as normal. I want this to happen in the same finger movement. (That is, the user should not need to lift the finger).</p>
  
  <p>I have implemented this using a UIPanGestureRecognizer on top of the UIWebView (see code below). The gesture handler moves the UIWebView and constantly resets the scrollview's contentOffset to 0. It works, but the performance is bad, especially if the webview contains a heavy page. I guess it is the scroll and reset of the webview that makes the rendering heavy. My solution kind of smells bad and I assume there must be a better way?</p>
  
  <p><strong>Do you know any better solution to achieve what I'm trying to do?</strong></p>
  
  <pre><code>@interface ViewController ()
  @property (strong, nonatomic) IBOutlet UIWebView *webView;
  @property (nonatomic, strong) UIPanGestureRecognizer *panGestureRecognizer;
  @property(nonatomic, strong) UIScrollView *webViewScrollView;
  @property (strong, nonatomic) IBOutlet NSLayoutConstraint *webViewTopMarginConstraint;
  @end
  
  @implementation ViewController {
      BOOL gestureEnabled;
  }
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      gestureEnabled = YES;
  
      // Initially put the webview 320 px down on the topview
      self.webView.translatesAutoresizingMaskIntoConstraints = NO;
      self.webViewTopMarginConstraint.constant = 320.0;
  
      // SET UP GESTURE RECOGNIZER
      _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesturesForWebview:)];
      _panGestureRecognizer.minimumNumberOfTouches = 1;
      _panGestureRecognizer.maximumNumberOfTouches = 1;
      _panGestureRecognizer.delegate = self;
      [self.webView addGestureRecognizer:_panGestureRecognizer];
  
      // Get hold of the webviews scrollview
      for (UIView* subView in self.webView.subviews) {
          if ([subView isKindOfClass:[UIScrollView class]]) {
              _webViewScrollView = (UIScrollView *)subView;
          }
      }
  
      // Load a web page in the webview
      NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.macrumors.com"]];
      [self.webView loadRequest:requestObj];
  }
  
  - (void)handlePanGesturesForWebview:(UIPanGestureRecognizer *)gesture {
  
      if (gesture.state == UIGestureRecognizerStateChanged) {
          CGFloat transY = [gesture translationInView:self.view].y; //transY will be negative by the number of pixels panned when panning upwards
          if (gestureEnabled) {
              // Move the whole webview according to gesture movement upwards
              // todo Handle scrolling in the opposite direction
              self.webViewTopMarginConstraint.constant = 320 + transY;
              if (self.webViewTopMarginConstraint.constant &lt;= 0.0) {
                  // The webview is at the top, disable the gesture recognizer
                  gestureEnabled = NO;
                  self.webViewTopMarginConstraint.constant = 0.0;
                  self.panGestureRecognizer.delegate = nil;
              }
              // "Rewind" the web scrolling
              CGPoint offset = CGPointMake(0.0, 0.0);
              [self.webViewScrollView setContentOffset:offset animated:NO];
          }
      }
  }
  
  #pragma mark UIGestureRecognizerDelegate impl
  - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
      return YES;
  }
  @end
  </code></pre>
  
  <p><strong>EDIT: Using insets as @JPHribovsek suggests in the accepted answer works perfectly. The code also became simpler. You can have a look at the solution here:</strong> <a href="http://gist.github.com/melke/8684172" rel="nofollow">http://gist.github.com/melke/8684172</a></p>