title: Why setting pagingEnabled to YES in UIScrollView automatically scrolls to previous page when the sub views are touched?
body: <p>I am using UIScrollView to show lots of subviews. Below is the code for creating scroll view:</p>
  
  <pre><code>ScrollView = [[UIScrollView alloc]initWithFrame:CGRectZero];
  ScrollView.delegate = self;
  ScrollView.showsHorizontalScrollIndicator = NO;
  ScrollView.showsVerticalScrollIndicator = NO;
  ScrollView.clipsToBounds = YES;
  ScrollView.scrollEnabled = YES;
  ScrollView.pagingEnabled = YES;
  
  CGFloat widthOfScroll= (maxTotalColumnsVisibleAtAnyPoint * widthOfSubView );
  
  ScrollView.frame = CGRectMake(0, 0, widthOfScroll, heightOfSubView);
  
  CGSize contentSizeHeaderView =  self.ScrollView.frame.size;
  CGFloat contentSizeWidth =  self.totalSubViews * self.widthOfSubView;
  contentSizeHeaderView.width =  contentSizeWidth;
  
  [ScrollView setContentSize:contentSizeHeaderView];
  </code></pre>
  
  <p>THe code for adding controls to ScrollView  and event handler for scrollView are below::</p>
  
  <pre><code>-(void)loadControlsInScroll{
  
  
      UIView *tempSubView = nil;
      CGFloat tempOriginX = 0;
      @autoreleasepool {
  
  
          for (int i = 0; i &lt; self.totalSubViews; i++) {
  
  
              tempSubView = [dataSource viewForScroll:i];
              tempSubView.layer.borderColor = borderColor.CGColor;
              tempSubView.layer.borderWidth = kBorderWidth;
              tempSubView.frame = CGRectMake(tempOriginX, 0, widthOfSubView, heightOfSubView);
              tempOriginX = tempOriginX + widthOfSubView;
  
              [self.ScrollView addSubview:tempSubView];
          }
  
      }
  
  }
  
  -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
  ///Event handler
  }
  </code></pre>
  
  <p>I have enabled paging in this scroll view. </p>
  
  <p>Let us assume that at max scrollView can hold 8 subviews in its frame. </p>
  
  <p><strong>CASE 1:</strong></p>
  
  <p>16 subviews are added to the scrollview. Now two page are displayed. Scrolling to both pages are working correctly. Touching subviews in both pages does not cause any incorrect behavior.</p>
  
  <p><strong>CASE 2:</strong></p>
  
  <p>9 subviews are added to the scroll view. Now also it is tow paged. First pages contain 1st-8th subviews. Page two contain 2nd-9th subviews.
  Now touching subviews in Page 1 works normal. Touching in Page 2 calls the event handler scrollViewDidScroll of UIScrollView.</p>
  
  <p><img src="https://i.stack.imgur.com/Iq13b.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/h97gs.png" alt="enter image description here"></p>
  
  <p><strong>MORE INFORMATION:</strong></p>
  
  <ol>
  <li>This does not happen if PagingEnabled is set to NO.</li>
  </ol>
  
  <p>Why this scrollViewDidScroll event gets called even though no scrolling was manually done? The event handler scrollViewWillBeginDragging is also called at this time?</p>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>The real problem is that if PagingEnabled is YES, then whether delegate of scrollview is set or not, in case 2, the scroll view automatically scrolls to the previous page.</p>