iphone - 锁定 ViewController 的方向

标签 iphone objective-c ios cocoa-touch

我的应用程序支持所有类型的方向,但我希望某些 viewController 仅支持横向,很少有人锁定 Potrait 模式。 我该怎么做,我试过下面的代码但没有任何效果。

- (BOOL)shouldAutorotate {
    return NO;
}

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

最佳答案

推送到同一个导航 Controller 的所有 View Controller 必须支持相同的方向。具体来说,我相信只有 Root View Controller 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];
}

关于iphone - 锁定 ViewController 的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14174449/

相关文章:

ios - NSFetchedResultsController 不知道数据已被 NSBatchUpdateRequest 更改

ios - 在 SpriteKit 中实现无限/重复滚动世界的最佳方法是什么?

ios - RLMException',原因 : 'Property ' productos' requires a protocol defining the contained type - example: RLMArray

ios - Init 方法未显示在 Swift Bridging header 中

ios - 压缩build设置中缺少的 Png 文件

objective-c - 当委托(delegate)不再被引用时,在 `[NSApp run]` 处崩溃

ios - 通过蓝牙 4.0 LE 发送图像文件

iphone - AVMutableComposition - 是否可能有 2 个并行音轨?

iphone - iAd 横向奇怪的 View 行为

iphone - 在 Storyboard 中使用自定义 Push Segue