首页 文章

支持iPhone上的纵向方向的通用应用程序和iPad上的横向纵向

提问于
浏览
5

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

在iPad中我需要它在Landscape和Portrait上都可用 . 在iPhone中虽然我需要rootView是Portrait本身,但我确实有一些viewsControllers,它们在tabbarController上呈现,需要在横向和Portrait中可用(例如用于播放Youtube视频的viewController) . 所以我按如下方式锁定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];
}

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

我已经经历了this question herehere,但还是无法正常工作 .

谢谢

1 回答

  • 4

    这就是我设法让它运作的方式 . 在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中描述的正确方向

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

相关问题