简体   繁体   English

如何在iOS 8中的模态视图控制器中强制纵向方向?

[英]How do I force portrait orientation in a modal view controller in iOS 8?

Here's my view controller code for the view controller that I only want to display in portrait orientation... 这是视图控制器的视图控制器代码,我只想以纵向显示...

- (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;
}

and here's how I'm presenting it... 这是我如何呈现它...

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

If the device is in landscape orientation and the view controller is presented, it correctly autorotates to portrait orientation. 如果设备处于横向并且显示视图控制器,则它会正确地自动旋转到纵向。 But if you rotate the device after it's displayed, it autorotates to whatever orientation the device is in. I want to prevent autorotation after its displayed and force only portrait orientation for just this one view controller. 但是如果在设备显示后旋转设备,它会自动旋转到设备所处的任何方向。我想在显示设备后防止自动旋转,并且仅针对此一个视图控制器强制纵向方向。 This is an iOS8-only app. 这是一款仅限iOS8的应用。

Thanks in advance for your wisdom! 提前感谢您的智慧!

The problem is that you are setting the supportedInterfaceOrientations for the wrong object. 问题是您正在为错误的对象设置supportedInterfaceOrientations You have implemented supportedInterfaceOrientations in MyViewController. 您已在MyViewController中实现supportedInterfaceOrientations But look at your code: 但看看你的代码:

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

nav is the presented view controller - not MyViewController. nav是呈现的视图控制器 - 而不是MyViewController。 So nav , the UINavigationController, governs whether we will rotate - not MyViewController. 所以nav ,UINavigationController,控制我们是否会旋转 - 而不是MyViewController。

So, give nav a delegate in which you implement navigationControllerSupportedInterfaceOrientations: - and all will be well. 因此,给nav一个委托,在其中实现navigationControllerSupportedInterfaceOrientations: - 一切都会好的。

You are returning the wrong value in your supportedInterfaceOrientations method. 您在supportedInterfaceOrientations方法中返回错误的值。

You need to return UIInterfaceOrientationMaskPortrait . 您需要返回UIInterfaceOrientationMaskPortrait

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS 7+关闭模态视图控制器和强制纵向方向 - iOS 7+ Dismiss Modal View Controller and Force Portrait Orientation 模态视图控制器强制iOS 6中的横向方向 - Modal View Controller force Landscape orientation in iOS 6 iPhone iOS 6 UITabBarController仅限于纵向,如何使Modal View Controller支持横向方向 - iPhone iOS 6 UITabBarController Portrait Only, How to have Modal View Controller support Landscape Orientation 如何在iOS 6中强制将方向设为纵向 - How to force Orientation as Portrait in iOS 6 强制iOS方向在一个模态视图控制器上横向移动 - Force iOS orientation to landscape on one modal view controller 如何在iOS 8中强制查看控制器方向? - How to force view controller orientation in iOS 8? iOS 9中的强制视图控制器方向 - Force View Controller Orientation in iOS 9 如何在iOS 6中强制UIViewController为Portrait方向 - How to force a UIViewController to Portrait orientation in iOS 6 如何从具有横向方向的视图控制器中以纵向显示新的纵向视图控制器? - How can I modally display a new view controller with a portrait orientation, from a view controller that has a landscape orientation? 除模态视图控制器外的所有视图控制器中的纵向方向 - Portrait orientation in all view controllers except in modal view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM