0
votes

I am using Apple's PageControl sample code for horizontal scrolling in portrait layout and everything works fine except when the UIScrollView is first loaded, the initial page shown has either

 1)  The first page offset by 11 pixels at location (11,0) or
 2)  The second page offset by 19 pixels at location (301,0) or
 3)  The second page offset by 65 pixels at location (255,0) or
 4)  The first page offset by 81 pixels at location (81,0) or...

The overall structure of my Xcode project is:

UITabBarController -> UITableView -> UIViewController containing UIScrollView

I.E. selecting a tab on the UITabBarController brings up a UITableView. Selecting a cell in the UITableView brings up a view with a UIScrollView in it. The offset problem occurs when this UIScrollView first displays.

It appears that when UIScrollView first loads, scrollViewDidScroll is being called multiple times (34!) whereas in Apple's PageControl sample code, scrollViewDidScroll is not called when the UIScrollView first loads.

Once UIScrollView loads, then UIScrollView works fine and scrolling ends with the pages on the frame boundaries at multiples of 320. My UIScrollview frame has a width equal to the number of pages times 320. I have the UIScrollView inside of a UIView.

The answer to this post says that scrollViewDidScroll gets called every time the scroll bounds change, but as far as I can tell I am not changing the scroll bounds; I am keeping the frame size the same for each of the viewControllers I am loading into the UIScrollView.

I am using viewDidLoad instead of awakeFromNib as in Apple's PageControl sample code. Does anyone have any idea what could cause:

 A)  scrollViewDidScroll to get called multiple times when UIScrollView first loads?
 B)  why scrollViewDidScroll would end on offsets that are not multiples of 320?
 C)  is the problem that UITableView contains a UIScrollView and so when transitioning
     from UITableVIew to UIScrollView some parameters in the UITableView are being
     used by the UIScrollView?
3

3 Answers

0
votes

A scrollview's bounds change for every scroll movement. The bounds are essentially the currently visible rectangle in the scrollview.

Example:

frame: {0,0,320.0,480.0} contentSize: {320.0, 1000.0} contentOffset: {0, 100.0}

bounds will be: {0, 100.0, 320.0, 480.0}

So for every change to contentOffset the bounds change. This causes layoutSubviews and didScroll to be called continuously while scrolling.

0
votes

I found the solution. The problem was that in loadScrollViewWithPage I was adding the ViewController to the UIScrollView and then modifying the ViewController. By moving the addition of the ViewController to the end of loadScrollViewWithPage after I had finished modifying the controller the problem disappeared. So I moved the following line to the end of loadScrollViewWithPage:

         [self.scrollView addSubview:controller.view];
0
votes

Let me make it clear for you:

For A) That's right,scrollViewDidScroll might be called more than 2 times when UIScrollView first loads.

For B)1: If it ends on offsets that are not multiples of 320, you might have forgotten or
setting the wrong frames and content variables,you should do such as:

   myScrollView.contentSize = CGSizeMake(myScrollView.frame.size.width * testArray.count 
   ,myScrollView.frame.size.height/2);

   myScrollView.contentOffset = CGPointMake(0, myScrollView.frame.origin.y);

   2: Then for the custom view you want to add into scrollview, you should set its frame like:
   Customview : CGRectMake(n*myScrollView.frame.size.with, 0, myScrollView.frame.size.with, 
   myScrollView.frame.size.height)];

   That's to say, when the width of custom view is same  with your scrollview, you will get the effect that you want.

For C) That's not right, forget that.