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

时间:2016-07-25 09:28:34

标签: ios objective-c xcode

我想在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];
});

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

ANSWER

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

  1. 当我启动应用时,它会处于纵向模式,

  2. 启动后,vcB首先是我调用的VC,

  3. 手机保持横向状态,因此当vcB中的viewDidAppear时,它会通话 旋转到风景

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

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

  6. 然后发生了不好的情况!

  7. 解决方案

    只需在“vcB”中首次启动旋转,然后在viewDidAppear中,它只会调用present:vcA

    非常感谢!

2 个答案:

答案 0 :(得分:5)

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

  2. 查找部署信息

  3. 将设备方向检查为仅限肖像。

    enter image description here

  4. OR

    - (void)viewWillAppear:(BOOL)animated {
    
       [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]forKey:@"orientation"];
     }
    
    - (BOOL)shouldAutorotate{
        return NO;
     }
    

答案 1 :(得分:3)

  1. 选择您的项目 - > Target-> Project-> General
  2. 根据屏幕截图(部署中)
  3. 检查并取消选中
  4. 确保您需要全屏检查,否则您无法将应用提交到应用商店。
  5. enter image description here

相关问题