[英]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.