繁体   English   中英

强制iOS方向在一个模态视图控制器上横向移动

[英]Force iOS orientation to landscape on one modal view controller

我有一个支持iPad和iPhone的通用应用程序。 在iPad上,我支持所有方向;在iPhone上,我仅支持纵向。

我希望一个视图控制器(以模态显示)在iPhone上运行时能以设备所处的任何方向显示。我已经看到许多教程和SO帖子建议使用-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window应用程序委托方法,相对于旋转模态视图控制器,这似乎工作得很好。 但是,当我在横向模式下关闭模式视图时,整个应用程序仍保持横向模式。

理想情况下,一旦取消模式视图,应用程序应回到纵向模式。

经过调查,似乎在关闭视图控制器后未调用-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window回调,我不知道为什么。

在这里使用了示例应用程序,并且在关闭视图控制器时触发了回调,但我不知道为什么。 我可以看到的唯一区别是我的视图层次结构要复杂得多,并且我显示的是导航控制器,而不是显式的视图控制器,但是我看不到它会如何影响回调。

有什么解决方案的想法吗?

谢谢

您可以尝试使用此方法(在应用商店构建中工作):

[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
                               withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)];

在appdelegate中:

  - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{
// Get topmost/visible view controller
    UIViewController *currentViewController = [self topViewController];

//Hire check your needed device ore other things you need
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone )
    { 
     //device is iphone
         if ([currentViewController respondsToSelector:@selector(canRotate)]) 
         {
            // Unlock landscape view orientations for this view controller
            return UIInterfaceOrientationMaskAll;
         }
         return UIInterfaceOrientationMaskPortrait;
    }  
    else
    //device Ipad
    return UIInterfaceOrientationMaskAll;  
}

}
- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
    UITabBarController* tabBarController = (UITabBarController*)rootViewController;
    return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
    UINavigationController* navigationController = (UINavigationController*)rootViewController;
    return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
    UIViewController* presentedViewController = rootViewController.presentedViewController;
    return [self topViewControllerWithRootViewController:presentedViewController];
} else {
    return rootViewController;
}
}

在所需的视图中旋转以确保实施

  - (void)canRotate { }

这将允许您仅旋转iPhone设备所需的视图。

我在旧的stackoverflow帖子的帮助下获得了此代码,但现在找不到指向它的链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM