Peter Todd
Peter Todd

Reputation: 8651

UIScrollView doesn't work when view starts but does work after rotation

I have a UIScrollView to display a longer listing of textviews, fields and images. In the viewDidAppear I set the contentSize appropriately and I also add this code to didRotateFromInterfaceOrientation so that the UIScrollView contentSize resizes based on the orientation.

Problem is that when the view first appears the UIScrollView doesn't respond (I can't scroll) but after rotating the device the code in didRotateFromInterfaceOrientation is called and everything works perfectly. (If I start the app in landscape orientation I can only scroll a little bit to see what is normally displayed in portrait mode and not the extended content - ignores the scrollView I've defined)

The code is the same in viewDidAppear and didRotateFromInterfaceOrientation so why does the scrollView only respond after rotation? Any help appreciated.

The output from the NSLog messages in both methods is the same.

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;


- (void) viewDidAppear:(BOOL)animated{
   //If not an iPad set the scrollview to allow scrolling of view
   if (!([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)) {
      self.scrollView.contentSize= CGSizeMake(self.view.frame.size.width,1000);
      [self.scrollView setUserInteractionEnabled:YES];
   }
   if(self.scrollView.userInteractionEnabled){
    NSLog(@"DEBUG scrollview height : %f",self.scrollView.contentSize.height);
    NSLog(@"DEBUG scrollview width : %f",self.scrollView.contentSize.width);    
   }
}

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
//If not an iPad set the scrollview

    if (!([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)) {
        self.scrollView.contentSize= CGSizeMake(self.view.frame.size.width,1000);
    }
    if(self.scrollView.userInteractionEnabled){
       NSLog(@"DEBUG scrollview height : %f",self.scrollView.contentSize.height);
       NSLog(@"DEBUG scrollview width : %f",self.scrollView.contentSize.width);
    }
}

Upvotes: 3

Views: 1907

Answers (2)

Gangani Roshan
Gangani Roshan

Reputation: 583

Hey Please Try this code I think your problem is solve using this code.

Try didRotate method in fetch your device orientation is Landscape or portrait. and set frame based on that.

 - (void)viewDidAppear:(BOOL)animated {    
        if (!([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)) {
            self.scrollView.contentSize= CGSizeMake(self.view.frame.size.width,1000);
            [self.scrollView setUserInteractionEnabled:YES];
        } else {
            [_scrollView setScrollEnabled:YES];
            _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, 2320);
        }
        if(self.scrollView.userInteractionEnabled){
            NSLog(@"DEBUG scrollview height : %f",self.scrollView.contentSize.height);
            NSLog(@"DEBUG scrollview width : %f",self.scrollView.contentSize.width);
        } }

    -(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        if (fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            [_scrollView setContentSize:CGSizeMake(736, 1000)];

        } else {
            [_scrollView setContentSize:CGSizeMake(414 , 1200)];

        }
    }

Happy Coding.

Upvotes: 1

Cyrille
Cyrille

Reputation: 25144

You'd better change the contentSize in viewDidLayoutSubviews, which is called when appearing and when changing the orientation.

But don’t forget to call super first!

Upvotes: 3

Related Questions