如何使应用程序在iOS 6中完全正常工作以进行自动旋转?

Car*_*ina 17 uiviewcontroller autorotate ios ios6

在iOS6中,shouldAutorotateToInterfaceOrientation已弃用.我尝试使用 supportedInterfaceOrientationsshouldAutorotate 使应用程序正常工作自动旋转但失败了.

这个ViewController我不想旋转,但它不起作用.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?在此先感谢您的帮助!

Car*_*ina 38

弄清楚了.

1)子类化UINavigationController(层次结构的顶层视图控制器将控制方向.)确实将其设置为self.window.rootViewController.

- (BOOL)shouldAutorotate
{
    return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return self.topViewController.supportedInterfaceOrientations;
}
Run Code Online (Sandbox Code Playgroud)

2)如果您不希望视图控制器旋转

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

3)如果你想让它能够旋转

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(BOOL)shouldAutorotate
{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

BTW,根据您的需要,另一种相关方法:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
     return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

  • +1用一些很好的代码示例来回答你自己的问题 (2认同)
  • 它适用于iOS 5.1或更高版本.除非您的应用程序的部署目标是6.0.@voromax (2认同)