7

As the title says. My UIViewController will not rotate no matter what. When it loads shouldAutorotateToInterfaceOrientation is being called but after that it doesnt.

UPDATE 1:

It's a really really wierd problem. At least for me. And i ll try to explain everything.

It's a navigation based app. Every controller has

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return YES; 
}

Xcontroller is a child of Acontroller and it doesn't auto rotate. If Xcontroller become a child of Bcontroller then it will autorotate. So something is wrong with Acontroller. But Acontroller is identical (except its data) to Bcontroller.

Whats Wrong?

UPDATE 2:

I decided to recreate Acontroller. And it worked.I believe I was missing something stupid.

Duck
  • 34,902
  • 47
  • 248
  • 470
looneygrc
  • 181
  • 1
  • 3
  • 11
  • How did you implement the `-shouldAutorotateToInterfaceOrientation:`? – kennytm Mar 10 '10 at 12:49
  • - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return YES; } If that is what you mean :). – looneygrc Mar 10 '10 at 13:30

9 Answers9

15

I am not sure whether it's the same reason as your case. But I experienced the same thing. the shouldAutorotateToInterfaceOrientation was only called once in the beginning. After some serious debugging by taking code apart, I found that the reason is in my overridden init method. I had this before:

- (id)initWithAlbum:(PhotoAlbum *)theAlbum {
    if (self) {     
        self.photoAlbum = theAlbum;     
    }
    return self;
}

And then I changed to this

- (id)initWithAlbum:(PhotoAlbum *)theAlbum {
    if (self = [super init]) {      
        self.photoAlbum = theAlbum;     
    }
    return self;
}

Note: the only difference is I added [super init] to call the parent init. After this change, the rotation works well and the shouldAutorotateToInterfaceOrientation is being called everytime I rotate the screen. Hope this help.

h4xxr
  • 11,385
  • 1
  • 39
  • 36
DaiLak
  • 746
  • 7
  • 10
  • 1
    Hey DaiLak, you saved my life. I had the same problem as yours, and I had been struggling with it for hours. Thanks. – Cullen SUN Dec 30 '11 at 18:22
8

There can be several possible reasons your view controller does not rotate.

See Apple's official Q&A on this issue:

Why won't my UIViewController rotate with the device?

http://developer.apple.com/library/ios/#qa/qa2010/qa1688.html

Community
  • 1
  • 1
nybon
  • 8,894
  • 9
  • 59
  • 67
5

Apple Q&A has the detailed solution for the problem.

Why won't my UIViewController rotate with the device?

http://developer.apple.com/library/ios/#qa/qa1688/_index.html

If you add a viewcontroller.view to uiwindow, you should set this viewcontroller as rootviewcontroller.

[self.window addSubview: mainViewcontroller.view];
self.window.rootViewController=mainViewcontroller;
Vanguarder
  • 396
  • 3
  • 8
3

Also, make sure you don't have rotation lock on. I spent a good hour trying to figure out why my views stopped rotating. shouldAutorotateToInterfaceOrientation was being called only once at start up and when Game Center leaderboards/achievements were presented.

Blackmouth
  • 31
  • 1
3

I had the same issue - the reason was, that it was my first UIViewController, that i created on the fly in my ApplicationDelegate, added it's View to my UIWindow and immediately released it.

That's of course not correct as I just added the UIView of the UIViewController (retaining it) and than released the whole controller.

You should add your first UIViewController as an instance variable in Your ApplicationDelegate instead, and release it in Your ApplicationDelegate's dealloc-method.

Ganayan
  • 31
  • 1
1

In my case, the ViewController was inside a NavigationController which was used by a "parent" viewControlled that received the orientation changes.

What I did in this parent was:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{

    if(_navigationController){
        return  [_navigationController.topViewController shouldAutorotateToInterfaceOrientation: toInterfaceOrientation];
    }

    return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}

This way you can implement your own orientation change logic depending on the currently visible controller.

htafoya
  • 18,261
  • 11
  • 80
  • 104
0

I have the same problem but with two view controllers added to the application's UIWindow. The reason is The view controller's UIView property is embedded inside UIWindow but alongside an additional view controller

From Apple Technical Q&A

http://developer.apple.com/library/ios/#qa/qa1688/_index.html

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Basem Saadawy
  • 1,808
  • 2
  • 20
  • 30
0
  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return YES; } The above method if u using, you will able to call many time if u want with out any error.
Tirth
  • 7,801
  • 9
  • 55
  • 88
0

I think there is no strange behavior here, it is called only one which is right. There is no need to call more than one to decide if the device should rotate to a direction or not.

This method just ask if the device should rotate to a direction or not. If you want to handle the orientation change, you should register for the notification from the UIDeviceDidChangeOrientationNotification and override the following method:

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
    !isShowingLandscapeView)
    {
        [self presentModalViewController:self.landscapeViewController
                            animated:YES];
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait &&
             isShowingLandscapeView)
    {
        [self dismissModalViewControllerAnimated:YES];
        isShowingLandscapeView = NO;
    }
}

See more here.

Hoang Pham
  • 6,899
  • 11
  • 57
  • 70
  • Thanks for your answer. But i don't understand why in my other controllers shouldAutorotateToInterfaceOrientation is enough to rotate them. And for this specific controller isn't enough. Also in my other controllers everytime I rotate the device shouldAutorotateToInterfaceOrientation is being called. – looneygrc Mar 10 '10 at 17:22
  • In other words ...my controller should call shouldAutorotateToInterfaceOrientation everytime i rotate the device to check if the orientation is supported. Well ... my "problematic" controller calls it only when it loads and not when the device rotates. – looneygrc Mar 10 '10 at 17:27
  • When the device rotates, it askes the controller if it should support the rotation by querying the "shouldAutorotateToInterfaceOrientation:", if the answer is yes, then it will call the willRotateToInterfaceDuration:duration: to see if it needs to do any other modifications. – Hoang Pham Mar 10 '10 at 18:01
  • I understand that. But in my case it askes only when controller loads. If i rotate the device it never askes. Nvm ill recreate this controller and see if this will fix it. – looneygrc Mar 10 '10 at 23:22
  • as I told you, there are no reason for the iphone os to call this function more than 1 time. So you should look on elsewhere to see what you can do to handle the device orientation. – Hoang Pham Mar 11 '10 at 10:04
  • Ok.. got that. But why, in my case, it doesn't "auto" rotate? – looneygrc Mar 11 '10 at 11:18