UIScrollview content size when orientation changes

前端 未结 2 418
醉酒成梦
醉酒成梦 2021-01-19 05:01

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 (UIDeviceOri         


        
相关标签:
2条回答
  • 2021-01-19 05:26

    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.

    0 讨论(0)
  • 2021-01-19 05:26
    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

    0 讨论(0)
提交回复
热议问题