UIScrollview content size when changing orientation

I have a paginated scrollview. In viewDidLoad, I check if the current orientation is terrain, then I set its content size to 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, scroll scrolls are smoothed and there is no diagonal scroll.

but when changing orientation

I need to set the scroll frame and content again, and I set it as shown below.

 -(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 can’t understand why I have to set the height of the content size to 600 in landscape mode, and this is also not enough. and this adds another problem that scrollview starts to scroll diagonally, which I don't want, as it looks so weird. Can someone help me figure out where and what I don't see?

I set scrollview autoresist mask as

 [scroll setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight]; 

but changing it does not help.

+4
source share
2 answers

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

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

Instead of this frame size, change this as

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

And you need to set the size of the content in the content that you have in the scroll in any orientation.

+2
source
  • Do not use UIDeviceOrientation . Use UIInterfaceOrientation . DeviceOrientation has two additional options that you do not need here. ( UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown )

  • Return Yes from shouldAutorotateToInterfaceOrientation

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

  • Implement this method as follows.

     -(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 be,

    pageNumber = 2

    statusBarHeight = 20

+5
source

Source: https://habr.com/ru/post/1446633/


All Articles