0

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?

Xithias
  • 991
  • 1
  • 14
  • 32
  • in this case your ViewController is changed when you use presentModalViewController. so make it subView instead of presenting viewController. check out the viewController programming guide. – fibnochi Jan 31 '12 at 14:52
  • But I've got more than 100 different screens... Do I have to make all of them subviews? There must be some way to fix this... I still don't understand why this was working on iOS4 and now it doesn't... – Xithias Jan 31 '12 at 15:21
  • Is there any method called when I return to a view with this code? [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil]; – Xithias Jan 31 '12 at 17:30

2 Answers2

0

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.

Giuseppe Garassino
  • 2,272
  • 1
  • 27
  • 47
  • I tried with addsubview and removesubview but doesn't work... the view added doesn't react properly to the orientation changes. If i go to the added one on Landscape, it's added, but it appears on Portrait. So, I can see at the back the other view on Landscape, and the added view in front, vertical (but the device is landscape). – Xithias Jan 31 '12 at 17:24
  • Try a new project with just two views and all the possible combinations, it should work... – Giuseppe Garassino Jan 31 '12 at 17:34
  • I think I've fixed it. I'll post the solution tomorrow (I can't do it now, I have to wait 8 hours, says the forum :P). Thanks for your advice, I'll keep it in mind for the next project! – Xithias Jan 31 '12 at 17:51
  • I just send a notification to the notification center when I leave the 'child' view, as you said. The 'parent' view is the observer, and when it receives the signal, it runs the shouldAutorotateToInterfaceOrientation method to actualize correctly the view. I'll post the code tomorrow :D – Xithias Jan 31 '12 at 18:34
0

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.

Xithias
  • 991
  • 1
  • 14
  • 32