Why setting pagingEnabled to YES in UIScrollView automatically scrolls to previous page when the sub views are touched?

StackOverflow https://stackoverflow.com/questions/13529074

Question

I am using UIScrollView to show lots of subviews. Below is the code for creating scroll view:

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];

THe code for adding controls to ScrollView and event handler for scrollView are below::

-(void)loadControlsInScroll{


    UIView *tempSubView = nil;
    CGFloat tempOriginX = 0;
    @autoreleasepool {


        for (int i = 0; i < 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
}

I have enabled paging in this scroll view.

Let us assume that at max scrollView can hold 8 subviews in its frame.

CASE 1:

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.

CASE 2:

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.

enter image description here enter image description here

MORE INFORMATION:

  1. This does not happen if PagingEnabled is set to NO.

Why this scrollViewDidScroll event gets called even though no scrolling was manually done? The event handler scrollViewWillBeginDragging is also called at this time?

UPDATE 1:

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.

Was it helpful?

Solution

The problem is that Paginenabled property of UIScrollView always scroll the scrollview to width of its bounds. So When you have 9 subviews it tries to scroll to 2nd page and shows you 2-9 subviews. In actual your page1 has subviews 1-8 and 2nd pgae has only 9 number subview. So The real problem is that UIScrollview's content width should be proportional to your number of Pages. In your case your 1 page holds 8 subviews. so content size should be Width of UIScrollview * no. of Pages.

// noOfpages = No of pages in scrollview (Each page has 8 subviews)
// noOfSubviews
int noOfpages = 
if(noOfSubviews%8==0){
   noOfPages = noOfSubviews/8;

}else{
   noOfPages = noOfSubviews/8 + 1;
}
scrollview.contentSize = CGSizeMake(scrollview.frame.size.width*noOfPages,scrollview.frame.size.height); 

In this way scrolling on first page will take you to view 9. and scrolling back will take you back to subview 1-8.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top