ios - 如何强制 View Controller 保持纵向模式?

标签 ios xcode rotation screen-orientation landscape-portrait

我有一个带有 Storyboard的 iOS 应用程序。我希望我的最后一个 View Controller 始终保持纵向模式。我一直在阅读,我发现自从

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

已弃用我应该使用其他方法,例如

-(BOOL)shouldAutorotate  
-(NSInteger)supportedInterfaceOrientations
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

但是我已经尝试了很多这种方法的组合,但我还是做不到。那么有人可以告诉我正确的方法吗?

最佳答案

由于您的 UIViewController 嵌入在 UINavigationController 中,除非您自己转发调用,否则它永远不会被调用。 (我认为 UINavigationController 有点缺陷)

像这样子类化 UINavigationController:

@interface RotationAwareNavigationController : UINavigationController

@end

@implementation RotationAwareNavigationController

-(NSUInteger)supportedInterfaceOrientations {
    UIViewController *top = self.topViewController;
    return top.supportedInterfaceOrientations;
}

-(BOOL)shouldAutorotate {
    UIViewController *top = self.topViewController;
    return [top shouldAutorotate];
}

@end

关于ios - 如何强制 View Controller 保持纵向模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16720968/

相关文章:

iOS:Instruments 显示 imageio_png_data 的大小比其实际图像大小大 300 倍

ios - UML 类图中的 Objective C block

c++ - 偏航、俯仰和滚动到 glm::rotate

IOS 轮换通知不起作用

iphone - iPhone:如何从JSON请求获取地址?

ios - 如何在 ios 中从 swift 和 alamofire 中的 json 下载 pdf 文件?

objective-c - 滚动到底部时向 UITableView 添加行

ios - 在 Swift 2 中捕获 "IndexOutOfBoundsException"

iphone - 如何从字典中检索数组,然后在该数组中检索字典?

安卓动画 "Rolling in"