我想将UIViewController设置为纵向模式,我尝试添加
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate {
return [UIApplication sharedApplication].statusBarOrientation != UIDeviceOrientationPortrait;
}
但是当我将手机横向拿起,然后进入这个UIViewController时,
我会先进入横屏,然后旋转到竖屏!
但我不想显示横屏,我想立即显示竖屏,
是否有错误的设置?
编辑
我将错误的ViewController称为"vcA",它从"vcB"呈现出来
在vcB的viewDidAppear
中,我调用了
[self presentViewController:vc animated:NO completion:nil];
然后如果vcB处于横屏状态,则vcA将以横屏显示,
如果稍微延迟一下,比如:
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,
手机保持横向状态,所以当
viewDidAppear
在vcB中调用时,它会调用旋转到横向同时在viewDidAppear中,我还调用了
present:vcA
因此,当前视图必须同时执行旋转到横向和呈现vcA(应该是纵向!!)
然后发生了糟糕的情况!
解决方案
只需在“vcB”中禁用第一次启动时的旋转,然后在viewDidAppear
中,它将仅调用present:vcA
非常感谢大家!