Support for portrait-oriented universal app on iPhone and landscape + portrait on iPad

I need my app to be compatible with both iPad and iPhone. It has a tabbarController as rootViewController.

On the iPad, I need it to be available for both landscape and portrait. On the iPhone, although I need a rootView for the portrait itself, and I have several viewsControllers that are displayed in the tabbarController, which should be available in both landscape and portrait orientation (for example, the viewController used to play video from Youtube). Therefore, I block the rotation of the tabbarController as follows (in a subclass of UITabbarController).

# pragma mark - UIRotation Methods - (BOOL)shouldAutorotate{ return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad); } - (NSUInteger)supportedInterfaceOrientations{ return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? UIInterfaceOrientationMaskAll : UIInterfaceOrientationMaskPortrait; } 

What I intend to do is that by blocking the rotation of the rootviewController (tabbarController), I block all the VCs in the tabbarController (iPhone only) and the views that appear on top of the tabbarController can rotate according to the orientation of the device.

PROBLEM

Everything works as expected until the application runs in the iPhone landscape. When launched in landscape mode, the application uses landscape by default and launches the application in landscape mode, which is not intended. It should start in portrait mode itself, even if the orientation of the device is Landscape. Since I turned off automatic rotation for the iPhone, the application continues to remain in the landscape, which leads to an error. I tried this method to make the application run in portrait in the application: didFinishLaunchingWithOptions:

 #pragma mark - Rotation Lock (iPhone) - (void)configurePortraitOnlyIfDeviceIsiPhone{ if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)) [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; } 

However, the problem persists. I have enabled all info.plist targeting options for the SupportedInterfaceOrientaions key for both iPad and iPhone, since I need the application to be in the landscape for iPhone, at least for several viewControllers. The problem can be fixed if I can somehow make this application run in portrait orientation, even if the device’s orientation is Landscape. Please correct me if I am wrong in the logic, if not, any help to make launching the application in portrait mode will be appreciated.

I have already addressed this issue here and here , but have not yet been able to launch it.

thanks

+5
source share
1 answer

Here's how I managed to get it to work. In AppDelegate.m I added this method.

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ //if iPad return all orientation if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)) return UIInterfaceOrientationMaskAll; //proceed to lock portrait only if iPhone AGTabbarController *tab = (AGTabbarController *)[UIApplication sharedApplication].keyWindow.rootViewController; if ([tab.presentedViewController isKindOfClass:[YouTubeVideoPlayerViewController class]]) return UIInterfaceOrientationMaskAllButUpsideDown; return UIInterfaceOrientationMaskPortrait; } 

This method checks every time a view for orientation is displayed and adjusts the orientation as necessary. I am returning the entire orientation for the iPad, and none of them for the iPhone excludes the view that should be presented (the one that should rotate, YouTubeVideoPlayerViewController) is disabled.

And in a subclass of tabbarController

 # pragma mark - UIRotation Methods - (BOOL)shouldAutorotate{ return YES; } - (NSUInteger)supportedInterfaceOrientations{ return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? UIInterfaceOrientationMaskAll : UIInterfaceOrientationMaskPortrait; } 

The problem was that when we return no to shouldAutoRotate, the application will ignore all rotation change notifications. It must return YES so that it turns to the correct orientation described in supportedInterfaceOrientations

I believe that this is how we should approach this requirement, and not pass rotation instructions to the appropriate viewControllers, as many posts said on SO. This is the advantage of using containers as recommended by Apple, so we don’t need to code the rotation instructions for each type in the container.

+4
source

All Articles