ios - 如何在 iOS 8 的模态视图 Controller 中强制纵向显示?

标签 ios objective-c iphone ios8

这是我只想以纵向显示的 View Controller 的 View Controller 代码...

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}

- (BOOL)shouldAutorotate {
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

下面是我介绍它的方式...

MyViewController *myVC = [[MyViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:myVC];
[self presentViewController:nav animated:YES completion:nil];

如果设备处于横向并且显示了 View Controller ,它会正确地自动旋转到纵向。但是,如果您在显示后旋转设备,它会自动旋转到设备所处的任何方向。我想在显示后防止自动旋转,并仅对这个 View Controller 强制为纵向。这是一款仅限 iOS8 的应用。

提前感谢您的智慧!

最佳答案

问题是您为错误的对象设置了 supportedInterfaceOrientations。您已经在 MyViewController 中实现了 supportedInterfaceOrientations。但是看看你的代码:

MyViewController *myVC = [[MyViewController alloc] init];
UINavigationController *nav = 
    [[UINavigationController alloc] initWithRootViewController:myVC];
[self presentViewController:nav animated:YES completion:nil];

nav 是呈现的 View Controller - 而不是 MyViewController。所以 nav,即 UINavigationController,控制着我们是否会旋转——而不是 MyViewController。

因此,给 nav 一个委托(delegate),您可以在其中实现 navigationControllerSupportedInterfaceOrientations: - 一切都会好起来的。

关于ios - 如何在 iOS 8 的模态视图 Controller 中强制纵向显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27882710/

相关文章:

ios - 使用 UIRefresh 更新 TableView

ios - 如何在 UIImageView 中显示 UIImage?

objective-c - 为什么静态库有一个带有 init 方法的类?

javascript - 如何使用react-native-fetch-blob将文件从应用程序文档文件夹移动到Iphone的文档文件夹

iOS无法将字典值获取到数组

ios - 我无法将 NSLogger 成功集成到我的应用程序中

ios - 如何为 iOS 视网膜显示器设置 Flot

iphone - 委托(delegate)不工作

ios - 通过 POST 登录后如何到达下一页

iphone - 从 NSString 的第一行删除换行符