Supporting Universal App with Portrait Orientation on iPhone and Landscape+Portrait on iPad

前提是你 提交于 2019-12-04 07:11:25

This is how I managed to get it working. In the 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 is displayed for the orientation and corrects orientation as required. I return all orientation for the iPad and none for the iPhone with exception that the view which is to be presented (the one which should rotate, YouTubeVideoPlayerViewController) is left off.

And in the tabbarController subclass,

# 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 app will ignore all rotation change notifications. It should return YES so that it will rotate to the correct orientation described in supportedInterfaceOrientations

I suppose this is how we should approach this requirement rather than passing on the rotation instructions to the respective viewControllers as many posts have said on SO. This is some advantage of using containers as recommended by Apple so that we don't have to code rotation instructions on each and every view in a container.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!