Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get launch orientation of iPad app

Tags:

In my iPad app, I need to run some layout code to set the proper layout depending on the orientation. By default, the layout is configured for the landscape orientation, so in the case that the app starts in portrait mode, I need to take extra action to configure the views properly for display in portrait.

In my -application:didFinishLaunchingWithOptions: method, I check the orientation using [[UIDevice currentDevice] orientation]. The problem here is that it always returns portrait even if the app is starting in landscape. Is there any way around this?

like image 339
indragie Avatar asked Aug 04 '10 03:08

indragie


People also ask

How do I get apps to rotate on my iPad?

Make sure that Rotation Lock is off: Swipe down from the top-right corner of your screen to open Control Center. Then tap the Rotation Lock button to make sure it's off.

How do I get my apps to rotate?

To enable auto rotate, you'll need to download the latest Google app update from the Play store. Once its installed, long-press on the home screen and tap on Settings. At the bottom of the list, you should find a toggle switch to enable Auto Rotation.

Do all apps rotate on iPad?

Not all iPad apps support orientation changes, so if the screen doesn't rotate, click the iPad's Home button to reach the main screen, and then try turning the device.


2 Answers

This is expected behavior. Quoth the UIViewController documentation:

Note: At launch time, applications should always set up their interface in a portrait orientation. After the application:didFinishLaunchingWithOptions: method returns, the application uses the view controller rotation mechanism described above to rotate the views to the appropriate orientation prior to showing the window.

In other words, as far as the device is concerned the orientation is portrait while the application is launching. At some point after application:didFinishLaunchingWithOptions: it will detect the different orientation and call your shouldAutorotateToInterfaceOrientation: method and then your other view rotation methods, which you should handle as normal.

like image 170
Anomie Avatar answered Sep 23 '22 20:09

Anomie


This is the best way to check for orientation on launch. First, create a new method in your AppDelegate that checks the orientation:

-(void)checkLaunchOrientation:(id)sender{       UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;          BOOL isLandscape = UIDeviceOrientationIsLandscape(self.viewController.interfaceOrientation);       if (UIInterfaceOrientationIsLandscape(orientation) || isLandscape) {        //do stuff here      } } 

At the end of -application:didFinishLaunchingWithOptions: run

        [self performSelectorOnMainThread:@selector(checkLaunchOrientation:) withObject:nil waitUntilDone:NO]; 
like image 41
Dillon Avatar answered Sep 22 '22 20:09

Dillon