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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:04:38  来源:igfitidea点击:

how to set orientation in Portrait only?

iosobjective-cxcode

提问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!

终于,我找到了根本原因!

  1. when I launch app, it stay in Portrait mode,

  2. After launch, vcB is first VC I called,

  3. Phone stay in landscape, so when viewDidAppearin vcB, it call rotate to Landscape

  4. At same time in viewDidAppear, I also call present:vcA

  5. So currently, view have to do both rotate to landscape and present vcA(should in Portrait!!)

  6. Then the bad situation happened!

  1. 当我启动应用程序时,它保持纵向模式,

  2. 启动后,vcB是我调用的第一个VC,

  3. 手机保持横向,所以viewDidAppear在 vcB 时,它调用旋转到横向

  4. 同时在viewDidAppear中,我也调用 present:vcA

  5. 所以目前,视图必须同时旋转到横向和呈现 vcA(应该在纵向!!)

  6. 然后糟糕的情况发生了!

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

  1. Go to General settings of your xcode project

  2. Find deployment info

  3. Check the device orientation to portrait only.

    enter image description here

  1. 转到 xcode 项目的常规设置

  2. 查找部署信息

  3. 仅检查设备方向为纵向。

    在此处输入图片说明

OR

或者

- (void)viewWillAppear:(BOOL)animated {

   [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]forKey:@"orientation"];
 }

- (BOOL)shouldAutorotate{
    return NO;
 }

回答by Rokon

  1. Select your project->Target->Project->General
  2. Check and uncheck according to the screenshot(Under Deployment)
  3. Make sure you check requires full screen or else you can not submit your app to app store.
  1. 选择您的项目->目标->项目->常规
  2. 根据截图勾选和取消勾选(部署中)
  3. 确保您检查需要全屏,否则您无法将您的应用程序提交到应用程序商店。

enter image description here

在此处输入图片说明