xcode 如何仅在纵向设置方向?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38564119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set orientation in Portrait only?
提问by Devin
I want to set UIViewController in Portrait, I tried to add
我想在人像中设置 UIViewController,我尝试添加
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate {
return [UIApplication sharedApplication].statusBarOrientation != UIDeviceOrientationPortrait;
}
But when I take my phone in Landscape, then enter this UIViewController,
但是当我在横向拿我的手机时,然后输入这个 UIViewController,
I will run into Landscape first, then rotate to portrait!
我会先遇到横向,然后旋转到纵向!
but I don't want it shows landscape, I want it show portrait immediate,
但我不希望它显示风景,我希望它立即显示肖像,
Is there any wrong setting?
有什么错误设置吗?
EDIT
编辑
I called the error ViewController "vcA", it present from "vcB"
我将错误视图控制器称为“vcA”,它来自“vcB”
in vcB's viewDidAppear
, I called
在 vcB 中viewDidAppear
,我打电话给
[self presentViewController:vc animated:NO completion:nil];
[self presentViewController:vc animated:NO completion:nil];
then vcA will show in landscape(if vcB was in landscape),
然后 vcA 将显示为横向(如果 vcB 为横向),
if add a few delay like:
如果添加一些延迟,例如:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self presentViewController:vc animated:NO completion:nil];
});
it will show portrait, but I do not know why it happen!
它会显示肖像,但我不知道为什么会这样!
ANSWER
回答
Finally, I found root cause!
终于,我找到了根本原因!
when I launch app, it stay in Portrait mode,
After launch, vcB is first VC I called,
Phone stay in landscape, so when
viewDidAppear
in vcB, it call rotate to LandscapeAt same time in viewDidAppear, I also call
present:vcA
So currently, view have to do both rotate to landscape and present vcA(should in Portrait!!)
Then the bad situation happened!
当我启动应用程序时,它保持纵向模式,
启动后,vcB是我调用的第一个VC,
手机保持横向,所以
viewDidAppear
在 vcB 时,它调用旋转到横向同时在viewDidAppear中,我也调用
present:vcA
所以目前,视图必须同时旋转到横向和呈现 vcA(应该在纵向!!)
然后糟糕的情况发生了!
Solution
解决方案
Just disable rotate for first launch in "vcB", then in viewDidAppear
, it will only call present:vcA
只需禁用旋转以在“vcB”中首次启动,然后在viewDidAppear
,它只会调用present:vcA
Thanks all a lot!
非常感谢大家!
回答by Akash KR
Go to General settings of your xcode project
Find deployment info
Check the device orientation to portrait only.
OR
或者
- (void)viewWillAppear:(BOOL)animated {
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]forKey:@"orientation"];
}
- (BOOL)shouldAutorotate{
return NO;
}