4

I have a scrollview with pagination. In viewDidLoad i check if the current orientation is landscape then i set its contentsize's height 440

 if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) 
    {
        [scroll      setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages,340)];


    }
    else if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]))

    {
        [scroll setFrame:CGRectMake(0,0,480,480)];
        [scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 440)];


    }

everything works fine scrollview scrolls smoothy and there is no diagonal scrolling.

but when orientation changes,

i have to set scrollview's frame and contentsize again and i am setting it as follow

-(void)orientationChanged:(id)object
{
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
    self.scroll.frame = [[UIScreen mainScreen]bounds];

    [scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 340)];
}


else
{
 self.scroll.frame = CGRectMake(0,0,480,480);
        [scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 600)];
}


}

i cant understand why i have to set content size's height upto 600 in landscape mode, and that too isnt enough. and it adds one more problem that scrollview starts scrolling diagonally which i dont want as it looks so weird. Can anyone help me understanding where and what i am missing?

i have set scrollview's autoresizing mask as

[scroll setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight];

but changing it doesnt help.

Vishal Singh
  • 4,400
  • 4
  • 27
  • 43
  • i just want to stop my scrollview's diagonal scrolling problem i dont care about other issue , i dont care if if i have to give content size height 1000. But i would like to understand the reason behind it. Diagonal scrolling only occurs when view changes its orientation to landscape when it is already present. It doesnt occur when view appears for the first time itself in landscape mode only. – Vishal Singh Nov 17 '12 at 12:12
  • I think you dont need to change the contentsize everytime when orientation changes. You just need to change only the frame size.Even if you using autoresizing , you dont need to do that too. – Dinesh Raja Nov 17 '12 at 13:12
  • [scroll setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth]; try like this – Dinesh Raja Nov 17 '12 at 13:18
  • I have to set its content size as i said if i dont increase my contentsize's height to 600 or more when orientation changes to landscape, i am not able to scroll vertically and see contents at the bottom of scroll view's frame. And i cant give scrollview flexible width as i have pagination,giving flexible width causes pages to overlap each other cuz scrollview stretches itself on both sides when you give flexible width. Thats why i am setting scrollview's frame everytime orientation changes – Vishal Singh Nov 17 '12 at 13:36

2 Answers2

5
  1. Don't use UIDeviceOrientation. Use UIInterfaceOrientation instead. DeviceOrientation has two extra options you don't need here. (UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown)

  2. Return Yes from shouldAutorotateToInterfaceOrientation

  3. Now willRotateToInterfaceOrientation: duration: will be called every time you rotate your device.

  4. Implement this method like this.

    -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
    CGRect frame;
    int pageNumber = 2;
    int statusBarHeight = 20;
    
    if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
        frame = CGRectMake(0, 0, 480, 320 - statusBarHeight);
    } else {
        frame = CGRectMake(0, 0, 320, 480 - statusBarHeight);
    }
    
    scrollView.frame = frame;
    scrollView.contentSize = CGSizeMake(frame.size.width * 2, frame.size.height);
    } 
    

    Let,

    pageNumber = 2

    statusBarHeight = 20

Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
  • Where is the shouldAutorotateToInterfaceOrientation that you mention in step 2? – Josh Jun 08 '15 at 11:48
  • @Josh `shouldAutorotateToInterfaceOrientation` is now deprecated. It was a `UIViewController's` method. Use `shouldAutorotate` and `supportedInterfaceOrientations` instead. – Warif Akhand Rishi Jun 08 '15 at 12:29
2

Here it is a problem in your code. Why you are setting the frame size like this?? You have only the screen size of 320px width. And when it changes to landscape, height will be only 320px. But you are setting the scroll height as 480px and it goes out of the screen and start to scroll diagonally.

self.scroll.frame = CGRectMake(0,0,480,480);

Instead of that frame size, change like this

self.scroll.frame = CGRectMake(0,0,480,320);

And you need to set the content size depend on the content you are having inside the scroll view in either orientation.

Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
  • 1
    thank you for the answer but i solved my problem by making parent scrollview for scrolling vertically and adding the scrollview which needs to scroll horizontally as subview of parentScroll. It works fine now but i think your answer could have also done the trick. – Vishal Singh Nov 18 '12 at 09:25