ios - 如何仅在纵向中设置方向?

标签 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];
});

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

回答

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

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

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

  3. Phone 保持横向,所以当 viewDidAppear 在 vcB 中,它调用 旋转到横向

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

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

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

解决方案

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

非常感谢!

最佳答案

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

  2. 查找部署信息

  3. 检查设备方向是否为纵向。

    enter image description here

- (void)viewWillAppear:(BOOL)animated {

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

- (BOOL)shouldAutorotate{
    return NO;
 }

关于ios - 如何仅在纵向中设置方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38564119/

相关文章:

ios7 navigationController pushViewController动画错误

ios - iPhone 上的 Swift playground

iphone - 移动包含 UIButtons 的 UIView 会使这些按钮停止响应点击

ios - -[PreviewViewController applicationWillSuspend] : message sent to deallocated instance 0x1806d9e0

iphone - UIView 控件在动画完成之前不绘制

ios - iOS8 中状态栏的黑色

swift - Swift 中的线性鼠标移动

ios - 如何从另一个 Controller 打开嵌入式 Segue Controller ?

IOS 剪贴板(复制粘贴)无法跨应用程序

ios - -[NSDictionary dictionaryWithContentsOfFile] 在 iPad 上返回 nil;在模拟器中返回字典