【问题标题】:Lock the orientation of ViewController锁定 ViewController 的方向
【发布时间】:2012-12-19 21:54:58
【问题描述】:

我的应用程序支持所有类型的方向,但是我希望某些 viewController 只支持横向,并且很少支持 Potrait 模式。 我该怎么做,我尝试了下面的代码,但没有任何效果。

- (BOOL)shouldAutorotate {
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
// pre-iOS 6 support
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

【问题讨论】:

  • 什么不起作用?使用您发布的代码,控制器是否旋转景观?您提到的不同控制器是如何连接的?
  • 通过导航控制器。控制器确实旋转到横向

标签: iphone objective-c ios cocoa-touch


【解决方案1】:

推送到同一个导航控制器的所有视图控制器必须支持相同的方向。具体来说,我相信只有根视图控制器shouldAutorotateToInterfaceOrientation 被考虑在内。您可以查看this S.O. post 以获得一种解决方法;或在this one 获取可能有所帮助的不同方法。

我在上面链接的第二篇文章建议对shouldAutorotateToInterfaceOrientation 使用此定义,您可以根据自己的情况对其进行调整:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
  {   
  if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
    UIViewController *rootController = [((UINavigationController *)self.selectedViewController).viewControllers objectAtIndex:0];
    return [rootController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
  }
  return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多