如何强制视图控制器保持纵向模式? [英] how to force view controller to stay in portrait mode?

查看:50
本文介绍了如何强制视图控制器保持纵向模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带故事板的 iOS 应用程序.我希望我的最后一个视图控制器始终处于纵向模式.我一直在阅读,我发现自从

I have an iOS application with storyboard. I want my last viewcontroller to stay always in portrait mode. I've been reading and I found that since

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

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

is deprecated I should use other methods like

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

但是我已经尝试了很多这种方法的组合,但我一直无法做到.所以请有人告诉我正确的方法吗?

but i have tried so many combinations of this methods and I have not been able to do it. So please can someone tell me the correct way?

推荐答案

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

Since your UIViewController is embedded in a UINavigationController it'll never get called unless you forward on the calls yourself. (A bit of a flaw in UINavigationController in my opinion)

子类 UINavigationController 像这样:

Subclass UINavigationController like this:

@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

这篇关于如何强制视图控制器保持纵向模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆