如何仅在肖像中设置方向?

Dev*_*vin 5 xcode objective-c ios

我想在Portrait中设置UIViewController,我试图添加

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return [UIApplication sharedApplication].statusBarOrientation != UIDeviceOrientationPortrait;
} 
Run Code Online (Sandbox Code Playgroud)

但是当我在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];
});
Run Code Online (Sandbox Code Playgroud)

它会显示肖像,但我不知道它为什么会发生!

回答

最后,我找到了根本原因!

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

  2. 发布后,vcB是第一个叫VC的VC,

  3. 手机保持在风景中,因此viewDidAppear在vcB中,它会调用旋​​转到风景

  4. 同时在viewDidAppear中,我也打电话 present:vcA

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

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

只需在"vcB"中禁用第一次启动旋转,然后进入viewDidAppear,它只会调用present:vcA

非常感谢!

Aka*_* KR 8

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

  2. 查找部署信息

  3. 检查设备方向仅限纵向.

    在此输入图像描述

要么

- (void)viewWillAppear:(BOOL)animated {

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

- (BOOL)shouldAutorotate{
    return NO;
 }
Run Code Online (Sandbox Code Playgroud)


小智 5

  1. 选择您的项目->目标->项目->常规
  2. 根据截图勾选和取消勾选(部署中)
  3. 确保您检查需要全屏,否则您无法将您的应用程序提交到应用程序商店。

在此处输入图片说明