【问题标题】:Supporting Universal App with Portrait Orientation on iPhone and Landscape+Portrait on iPad支持 iPhone 纵向和 iPad 横向+纵向的通用应用程序
【发布时间】:2015-06-14 08:18:52
【问题描述】:

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

在 iPad 中,我需要它在横向和纵向上都可用。 在 iPhone 中,虽然我需要 rootView 本身是 Portrait 并且我确实有一些视图控制器,它们在 tabbarController 上呈现,它们需要在横向和纵向中都可用(例如,用于播放来自 Youtube 的视频的视图控制器)。所以我锁定 tabbarController 的旋转如下(在 UITabbarController 子类中)。

# pragma mark - UIRotation Methods

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

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

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

问题

在应用程序在 iPhone 环境中启动之前,一切都按预期工作。当以横向模式启动时,应用程序默认为横向模式,并以非预期的横向模式启动应用程序。即使设备方向是横向,它也应该以纵向模式启动。由于我关闭了 iPhone 的自动旋转,因此应用程序本身继续处于横向状态,从而导致错误。我尝试了这种方法来强制应用在应用中纵向启动:didFinishLaunchingWithOptions:

#pragma mark - Rotation Lock (iPhone)

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

问题仍然存在。我已经为 iPad 和 iPhone 的 SupportedInterfaceOrientaions 键允许 info.plist 上的所有方向选项,因为我需要应用程序在 iPhone 上处于横向状态,即使只有几个视图控制器也是如此。如果我能以某种方式强制该应用程序以纵向方向启动,即使设备方向是横向,这个问题也可以解决。如果逻辑有误,请纠正我,如果没有,任何帮助使应用程序以纵向模式启动将不胜感激。

我已经使用了this question herehere,但还不能正常工作。

谢谢

【问题讨论】:

    标签: ios objective-c rotation uitabbarcontroller landscape-portrait


    【解决方案1】:

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

    - (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)被保留。

    并且在 tabbarController 子类中,

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

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

    我想这就是我们应该如何处理这个要求,而不是像许多帖子在 SO 上所说的那样将旋转指令传递给相应的视图控制器。这是使用 Apple 推荐的容器的一些优势,因此我们不必在容器中的每个视图上编写旋转指令。

    【讨论】:

    • 很好理解,我将执行此操作。谢谢,我正在寻找这种解决方案,我们可以在 AppDelegate 中实现,无需在所有视图控制器中编写。
    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多