将应用程序限制在IOS 5上的特定方向

将应用程序限制在IOS 5上的特定方向,ios,objective-c,ipad,ios5,orientation,Ios,Objective C,Ipad,Ios5,Orientation,我有一个使用Cocos2dx引擎开发的IOS应用程序 应用程序针对特定方向锁定(例如纵向) 应用程序中的所有内容似乎都运行良好,并且符合正确的方向,但最近的应用程序栏和通知除外,它们符合设备方向 我希望能够限制它,使其与应用程序本身具有相同的方向 我注意到,删除info.plist文件中的横向方向可以完成这项工作,但我希望能够通过代码完成这项工作 在IOS 6中,我发现我所需要做的就是在我的RootViewController 给人一个正确的方向,这就成功了, 但这种方法不适用于IOS 5及以下

我有一个使用Cocos2dx引擎开发的IOS应用程序

应用程序针对特定方向锁定(例如纵向) 应用程序中的所有内容似乎都运行良好,并且符合正确的方向,但最近的应用程序栏和通知除外,它们符合设备方向

我希望能够限制它,使其与应用程序本身具有相同的方向

我注意到,删除
info.plist
文件中的横向方向可以完成这项工作,但我希望能够通过代码完成这项工作

在IOS 6中,我发现我所需要做的就是在我的
RootViewController
给人一个正确的方向,这就成功了, 但这种方法不适用于IOS 5及以下版本

我需要做些什么才能在IOS 5及以下版本的设备上运行

这是
RootViewController
的代码(我没有编写它,我只是添加了最后一个方法,我正在尝试解决通知和最近的应用程序问题)


对于iOS 5使用shouldAutorotateToInterfaceOrientation,对于iOS 6使用shouldAutorotate。在iOS5方法中,对支持的方向使用if-case并返回YES。在应用程序摘要中,启用所有方向。希望这些将对您有所帮助。

我制作此代码框架用于处理想要的和不想要的设备方向,在我的情况下,我希望忽略UIDeviceOrientationUnknown、UIDeviceOrientationFaceUp和UIDeviceOrientationFaceDown,缓存最后允许的方向。这段代码涉及iPhone和iPad设备,对您很有用

- (void)modXibFromRotation {

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSString *device = [[UIDevice currentDevice]localizedModel];
UIInterfaceOrientation cachedOrientation = [self interfaceOrientation];

if ([device isEqualToString:@"iPad"]) {

    if (orientation == UIDeviceOrientationUnknown || 
        orientation == UIDeviceOrientationFaceUp || 
        orientation == UIDeviceOrientationFaceDown) {

            orientation = (UIDeviceOrientation)cachedOrientation;
    }

    if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

        /* Your code */
    }

    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {

        /* Your code */     
    }
}

if ([device isEqualToString:@"iPhone"] || [device isEqualToString:@"iPod"]) {

    if (orientation == UIDeviceOrientationUnknown || 
    orientation == UIDeviceOrientationFaceUp || 
    orientation == UIDeviceOrientationFaceDown) {

        orientation = (UIDeviceOrientation)cachedOrientation;
    }

    if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

         /* Your code */
    }

    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {

        /* Your code */
    }
  }
}

您必须从视图控制器中的两个位置调用此方法:

- (void)viewDidLoad {   

     [notificationCent addObserver:self
                          selector:@selector(modXibFromRotation)
                              name:UIDeviceOrientationDidChangeNotification object:nil];
}
首先开始在应用程序代理中生成设备定向通知:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

//**** ADD THIS CODE *****
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

// Add the main view controller's view to the window and display.
self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];

return YES;
}

然后在视图控制器中侦听设备方向通知:

- (void)viewDidLoad {   

     [notificationCent addObserver:self
                          selector:@selector(modXibFromRotation)
                              name:UIDeviceOrientationDidChangeNotification object:nil];
}
最后从以下位置调用modXibFromRotation方法:

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear];

    [self modXibFromRotation];
}

这将在显示视图之前调用检查方向方法。

1。如果状态栏和最近的应用程序栏的方向不同,那么iOS真的不知道你想锁定方向,2。在这个话题上,想详细解释一下为什么你的情况不同吗?我尝试了我在其他帖子中找到的所有东西,但似乎没有任何效果,所以你的apps Info.plist中只列出了一个方向,您在每个视图控制器中都实现了
shouldAutorotateToInterfaceOrientation
shouldAutoRotate
以及
支持的界面方向
?无论它在完成所有这些之后是否工作,这些信息(你都尝试过的)都需要在OP中。帮助我们帮助你,我们拥有的信息越多,我们就可以更好/更快地帮助你。我只有一个viewController,因为我在我的应用程序(游戏)中使用Cocos2dx。我的info.plist有4个方向,2个用于纵向,2个用于横向,当我删除2个横向时,它解决了我的问题,但我正在寻找一个解决方案,通过代码覆盖info.plist的值,这样我就不必触摸info.plist(我有我的理由),谢谢我尝试过IOS 6的自动旋转,但它没有工作,它当前返回“是”,更改为“否”对最近的应用程序栏没有影响。实现这个技巧的方法是实现preferredInterfaceOrientationForPresentation,以返回UIInterfaceOrientationGrait。但这不适用于ios 5尝试添加supportedInterfaceOrientations方法并返回您的方向。-(BOOL)应该自动旋转指针FaceOrientation:(UIInterfaceOrientation)到InterfaceOrientation{if(toInterfaceOrientation==UIInterfaceOrientation纵向| | toInterfaceOrientation==UIInterfaceOrientation纵向){return YES;}否则{return NO;}}上面的代码可以让你在iOS 5中以纵向和倒置纵向模式进行定向。编写你自己的关于我提供的代码的方法可能会对你有所帮助。我尝试了你的代码(没有加载我的配置,只是为了测试它是否限制为纵向)它什么也没做。当我运行游戏并且设备在横向中定向时,游戏以纵向方式打开,但当我双击home按钮时,LandscapeYo中最近打开的应用程序必须从以下位置调用此方法:-(void)viewswillbeen:(BOOL)animated{}你也可以监听UIDeviceOrientationIDChangeNotification的通知来触发它。在我的项目中100%有效。你能提供一个例子吗?再次检查我的答案,我为我的方法的实现添加了更多的代码