支持iPhone上的纵向方向的通用应用程序和iPad上的横向+纵向 [英] Supporting Universal App with Portrait Orientation on iPhone and Landscape+Portrait on iPad

查看:330
本文介绍了支持iPhone上的纵向方向的通用应用程序和iPad上的横向+纵向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的应用程序兼容iPad和iPhone。它有一个tabbarController作为rootViewController。

I need my app to be compatible both on iPad and iPhone. Its having a tabbarController as the rootViewController.

在iPad中我需要它在Landscape和Portrait上都可用。
在iPhone中虽然我需要rootView是Portrait本身,但我确实有一些viewsControllers,它们在tabbarController上呈现,需要在横向和Portrait中都可用(例如用于播放Youtube视频的viewController) )。所以我按如下方式锁定tabbarController的旋转(在UITabbarController子类中)。

In iPad I need it to be available both on Landscape and Portrait. In iPhone though I need the rootView to be Portrait itself and i do have some viewsControllers, which are being presented on the tabbarController, which need to be available both in landscape and Portrait (e.g. a viewController used to play videos from Youtube). So I am locking the rotation of the tabbarController as follows (In the UITabbarController Subclass).

# pragma mark - UIRotation Methods

- (BOOL)shouldAutorotate{
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
}

- (NSUInteger)supportedInterfaceOrientations{
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? UIInterfaceOrientationMaskAll : UIInterfaceOrientationMaskPortrait;
}

我打算做的是通过锁定rootviewController的旋转(tabbarController) ),我将所有VC锁定在tabbarController中(仅限iPhone),并且tabbarController顶部显示的视图可以根据设备方向旋转。

What I intend to do is that by locking the rotation of the rootviewController(tabbarController), I am locking all the VC's in the tabbarController(only on iPhone) and views which are presented on top of the tabbarController can rotate as per the device orientation.

THE问题

一切都按预期工作,直到应用程序在iPhone中的风景中启动。在横向模式下启动时,应用程序默认为横向显示并以横向模式启动应用程序,这不是预期的。即使设备方向为横向,它也应在纵向模式下启动。由于我关闭iPhone的自动旋转,应用程序继续在横向本身导致错误。我尝试使用此方法强制应用程序在应用程序中以纵向方式启动:didFinishLaunchingWithOptions:

Everything works as expected until the app is launched in the landscape in the iPhone. When launched in the landscape mode the app defaults to landscape and launches the app in the landscape mode which is not intended. It should launch in the Portrait mode itself even if the device orientation is Landscape. Since I am turning off auto rotation for iPhone the app continues to be in landscape itself resulting in a bug. I tried this method to force the app to launch in portrait in the application:didFinishLaunchingWithOptions:

#pragma mark - Rotation Lock (iPhone)

- (void)configurePortraitOnlyIfDeviceIsiPhone{
    if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone))
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
}

问题仍然存在。我已经在info.plist上为iPad和iPhone提供了SupportInterfaceOrientaions键的所有方向选项,因为我需要应用程序才能在iPhone中使用,即使只有少数几个viewControllers。如果我可以以某种方式强制该应用程序以纵向方向启动,即使设备方向为横向,也可以解决该问题。如果错误,请纠正我,如果没有,任何有助于以纵向模式启动应用程序的帮助将不胜感激。

Still the issue persists. I have allowed all orientation options on the info.plist for SupportedInterfaceOrientaions key both for iPad and iPhone since I need the app to be in landscape for iPhone even if only for a few viewControllers. The issue can be fixed if I can somehow force that app to launch in Portrait orientation even if the device orientation is Landscape. Please correct me if am wrong in the logic, if not, any help to make the app launch in the portrait mode will be appreciated.

我已经完成了这个问题在这里这里,但无法使其正常工作。

I have already gone through this question here and here, but couldn't get it working yet.

谢谢

推荐答案

这就是我设法让它发挥作用的方式。在AppDelegate.m中,我添加了这个方法。

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;
}

此方法每次为方向显示视图时都会检查,并将方向更正为需要。我将返回iPad的所有方向,而不是iPhone的方向,除了要显示的视图(应该旋转的视图,YouTubeVideoPlayerViewController)保持关闭。

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.

在tabbarController子类中,

And in the tabbarController subclass,

# pragma mark - UIRotation Methods

- (BOOL)shouldAutorotate{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? UIInterfaceOrientationMaskAll : UIInterfaceOrientationMaskPortrait;
}

问题是,当我们向shouldAutoRotate返回no时,应用程序将忽略所有旋转更改通知。它应该返回YES,以便它将旋转到supportedInterfaceOrientations中描述的正确方向

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

我想这是我们应该如何处理这个要求而不是将旋转指令传递给相应的viewControllers已经在SO上说了很多帖子。这是使用Apple推荐的容器的一些优点,因此我们不必在容器中的每个视图上编写旋转指令。

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.

这篇关于支持iPhone上的纵向方向的通用应用程序和iPad上的横向+纵向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆