gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:33:55 24 4
gpt4 key购买 nike

这是我只想以纵向显示的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com