我想在Portrait中设置UIViewController,我尝试添加
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate {
return [UIApplication sharedApplication].statusBarOrientation != UIDeviceOrientationPortrait;
}
但是当我在Landscape中拿手机的时候,然后进入这个UIViewController,
我会先遇到横向,然后旋转到纵向!
但我不希望它显示风景,我希望它立即显示肖像,
是不是设置错了?
编辑
我将错误 ViewController 称为“vcA”,它来自“vcB”
在 vcB 的 viewDidAppear
中,我调用了
[self presentViewController:vc animated:NO completion:nil];
然后 vcA 将横向显示(如果 vcB 横向显示),
如果添加一些延迟,例如:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self presentViewController:vc animated:NO completion:nil];
});
它会显示肖像,但我不知道为什么会这样!
回答
终于,我找到了根本原因!
当我启动应用时,它保持纵向模式,
启动后,vcB 是我调用的第一个 VC,
Phone 保持横向,所以当
viewDidAppear
在 vcB 中,它调用 旋转到横向同时在viewDidAppear中,我也调用了
present:vcA
所以目前, View 必须同时旋转到横向和呈现 vcA(应该是纵向的!!)
然后糟糕的情况发生了!
解决方案
只需在“vcB”中首次启动时禁用旋转,然后在 viewDidAppear
中,它只会调用 present:vcA
非常感谢!
最佳答案
或
- (void)viewWillAppear:(BOOL)animated {
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]forKey:@"orientation"];
}
- (BOOL)shouldAutorotate{
return NO;
}
关于ios - 如何仅在纵向中设置方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38564119/