6
votes

I'm creating a puzzle game for the iphone. All of my menus, etc are only in the portrait orientation, but my gameplay can be in either portrait or landscape.

So when I'm in the menu and the device is being held in landscape, then when I go to the gameplay viewcontroller, it does the layout with the landscape coordinates, but the screen is still actually oriented portrait-wise:

enter image description here

I want to force the device to re-orient the screen before I do the layout. How do I do that?

right now I'm testing for device orientation like this:

- (void) viewDidLoad {
    .
    .
    .

   if (!UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
      // Run Portrait Layout
   }
   else {
      // Run Landscape Layout
   }
}

So I know that the device is returning an orientation of landscape, but the screen is still oriented portrait for some reason.

2
Have you enabled the supported orientation in project settings? Maybe you're portrait-locked? - anon_dev1234
Thanks but No its not that. Once I'm in the GamePlay the rotations work fine. Its only if I load the VC while the Device is in landscape - bkbeachlabs
@aking63 Let me rephrase your question. Its like, if you open your game in Landscape mode and then change its orientation, then your view does not refresh. Right? So you want to refresh you view every time on change of orientation? - Sanchit Paurush
@sanchitsingh no its only the initial setup. As soon as I change the phone's orientation, it figures out what it should be doing. But even if its held in landscape during the pushViewController event, it loads with the screen facing portrait. - bkbeachlabs

2 Answers

18
votes

Perhaps try..

[UIViewController attemptRotationToDeviceOrientation];

In viewDidLoad or viewWillAppear, that should make it attempt an orientation change

1
votes

There are a few methods in UIViewController that you can override to catch the view-did-rotate event:

– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:

Do check the UIViewController class reference