在iOS6, shouldAutorotateToInterfaceOrientation 被弃用。我试着用 supportedInterfaceOrientationsshouldAutorotate 使应用程序正常工作的自动旋转,但失败。

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

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

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

有什么想法吗?提前感谢您的任何帮助!

有帮助吗?

解决方案

想通了。

1)子类UINavigationController (层次结构的顶部视图控制器将控制方向。) 确实把它设定为自我。窗户。rootViewController。

- (BOOL)shouldAutorotate
{
    return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return self.topViewController.supportedInterfaceOrientations;
}

2)如果你不想视图控制器旋转

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

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

3)如果你希望它能够旋转

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

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

顺便说一句,根据您的需要,另一种相关方法 :

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
     return UIInterfaceOrientationMaskPortrait;
}

其他提示

如果您使用标签栏控制器而不是导航控制器作为根控制器,则需要类似地将uitabbarcontroller子类化。

语法也会有所不同。我成功地使用了以下内容。然后,我在我想要复盖的视图控制器上成功地使用了上述示例。在我的情况下,我希望主屏幕不旋转,但我有一个FAQ屏幕与电影,我自然想启用景观视图。完美的工作!只需注意语法更改为self。modalViewController(如果您尝试使用导航控制器的语法,则会收到编译器警告。)希望这有助于!

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotate
{
    return self.modalViewController.shouldAutorotate;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return self.modalViewController.supportedInterfaceOrientations;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top