Scrollview not working on xcode4 (worked on xcode3)

272 views Asked by At

I'm a newbie, so forgive me if this is a silly doubt... I've recently changed my project from xcode3 to xcode4, and now there are some problems, and I don't know why...

To manage the frame and the content size of my scrollview, and its changes if the orientation changed, I've got this:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

 if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||

      toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)     {

      scrollView.frame = CGRectMake(0,0,480,300);

      [scrollView setContentSize:CGSizeMake(480,560)];

 }

 else {

      scrollView.frame = CGRectMake(0,0,320,460);

      [scrollView setContentSize:CGSizeMake(320,560)];

 }

 [scrollView flashScrollIndicators];}

I think that with Xcode3, the app called this method when the screen was loaded, so it was working perfectly in every situation.

But in Xcode4, the app doesn't call this method when the screen loads. It only calls it when the orientation changes (I don't know why exists this difference). So, my scrollview doesn't work when the screen is loaded. It only starts to work when I change the orientation of the device.

Then, I tried this:

- (void)viewDidLoad {

if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft ||

      [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)     {

      scrollView.frame = CGRectMake(0,0,480,300);

      [scrollView setContentSize:CGSizeMake(480,560)];

 }

 else {

      scrollView.frame = CGRectMake(0,0,320,460);

      [scrollView setContentSize:CGSizeMake(320,560)];

 }

[scrollView flashScrollIndicators];

 [super viewDidLoad];}

It was apparently working, BUT... something very strange happens...

If I go to another view (on Portrait orientation), and then I change the orientation to Landscape, and then I return to the first view (on Landscape), the autosizing mask of the view goes crazy. All the content goes to the right side, as if the view had more width than it actually has. And I can't access part of the content. But if now I change the orientation to Portrait, everything is OK again, even if I go to Landscape again. So, it's only wrong when I come to the view from another view on Landscape orientation.

This is how I pass from a view to another view:

- (IBAction) btnUnVano:(id) sender {


 PE2100UnVano *controller = [[PE2100UnVano alloc] initWithNibName:@"PE2100UnVano" bundle:nil];

 controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

 [self presentModalViewController:controller animated:YES];

 [controller release];}

What should I do?

Pd: Sorry for my English


EDIT

OK, this part is fixed now. I deleted the willAnimateRotationToInterfaceOrientation, and now it works correctly with this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Si la orientaciĆ³n es horizontal..
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft ||
    [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)   {
    scrollView.frame = CGRectMake(0,0,480,300);
    [scrollView setContentSize:CGSizeMake(480,560)];
}
// Si la orientaciĆ³n es vertical..
else {
    scrollView.frame = CGRectMake(0,0,320,460);
    [scrollView setContentSize:CGSizeMake(320,560)];
}
// Mostrar durante un instante los indicadores de scroll
[scrollView flashScrollIndicators];
// Retorna YES para soportar todas las orientaciones posibles
return YES;}

Now, there's another problem. In some screens, I return to the previous screen with this code:

- (IBAction) btnAtrasTabla:(id) sender {
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];}

In this case, the problem of the autosize appears again. I go to this screen on Portrait, I change to Landscape, I return to the previous, and the previous appears with a wrong autosize. Then, I change the orientation, and all is correct again... any ideas?

2

There are 2 answers

0
Xithias On BEST ANSWER

Here is what I finally did, with the advice of Beppe.

On the 'child' view, when I leave it, I send a notification:

[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"resize" object:nil userInfo:nil];

Then, the 'parent' view is the observer:

- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shouldAutorotateToInterfaceOrientation:) name:@"resize" object:nil];
[super viewDidLoad];}

Doing this, when we return to the 'parent' view from the 'child' view, the shouldAutorotateToInterfaceOrientation method is called. Remember that in this method, I resize everything to fit the current orientation of the app.

4
Giuseppe Garassino On

Any viewController.view added to your rootViewController inherits its behavior from the rootViewController itself.

[self.view addSubview:anotherViewController.view];

This means you need to enable shouldAutorotateToInterfaceOrientation only in your rootViewController.

If you present your viewController as a modal view, you have different behaviors.

[self presentModalViewController:anotherViewController animated:YES];

So you need to enable shouldAutorotateToInterfaceOrientation in your parent and in the modal view itself.

A correct architecture should solve your issue, but I think there is a workaround if you are not able to clean your code: check for the orientation in your current view and send a notification to other views, in order to set their frame. Remember that this workaround could be useful, but it is not recommended.