【问题标题】:How to make app fully working correctly for autorotation in iOS 6?如何使应用程序在 iOS 6 中完全正常运行以实现自动旋转?
【发布时间】:2012-09-21 15:37:12
【问题描述】:

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

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

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

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

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

【问题讨论】:

  • 视图控制器是嵌入在导航控制器还是标签栏控制器中?
  • 嵌入在导航控制器中。 @phix23

标签: ios uiviewcontroller ios6 autorotate


【解决方案1】:

想通了。

1) 子类 UINavigationController(层次结构的顶部视图控制器将控制方向。) 确实将其设置为 self.window.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;
}

【讨论】:

  • +1 用一些不错的代码示例回答您自己的问题
  • 你为什么要覆盖- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation?它已被弃用并且永远不会调用
  • 它适用于 iOS 5.1 或更高版本。除非您的应用的部署目标是 6.0。 @voromax
  • 不起作用。我的子类 UINavigationController 从未调用过 -(BOOL)shouldAutorotate。
  • 你设置成self.window.rootViewController了吗? @Zigglzworth
【解决方案2】:

如果您使用标签栏控制器而不是导航控制器作为您的根控制器,您将需要类似地继承 UITabBarController。

语法也会有所不同。我成功地使用了以下内容。然后,我在要覆盖的视图控制器上成功使用了上述示例。就我而言,我希望主屏幕不旋转,但我有一个带有电影的常见问题屏幕,我自然希望启用横向视图。工作完美!只需注意对 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多