【问题标题】:iphone always returns UIInterfaceOrientationPortraitiphone 总是返回 UIInterfaceOrientationPortrait
【发布时间】:2011-08-17 10:07:28
【问题描述】:

我需要确保当我的UIViewController 加载时,它会根据需要旋转。

我已经实现了shouldAutorotateToInterfaceOrientation 方法并且它一切正常,除了当应用程序第一次加载(当 iphone 处于横向模式时)或从 UIViewController 这是纵向模式时,interfaceOrientation 和 @ 987654324@ 总是返回纵向模式!

我一直在阅读博客甚至一些 SO 问题,但似乎都没有解决根本问题。

我正在使用标准 UINavigationController 应用程序。所以第一个视图是第一个压入堆栈的控制器。我也试过询问 self.navigationController 的方向,也没有运气。

非常感谢这里的任何帮助。

编辑:这是我的 viewWillAppear 代码

- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self navigationController] setNavigationBarHidden:YES animated:NO];

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    [self setPortraitMode];
else
    [self setLandscapeMode];
}

编辑 2

这是我打印当前的方式

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
NSLog(@"currentDevice: %d", (int)orientation);
NSLog(@"statusbar: %d", (int)[UIApplication sharedApplication].statusBarOrientation);
NSLog(@"nav controller: %d", (int)self.navigationController.interfaceOrientation);
NSLog(@"interface orientation: %d", (int)self.interfaceOrientation);

他们都打印出“1”

但是当我将此代码放入 willRotateToInterfaceOrientation 方法时,它们会打印出正确的 int 值(1、2、3 或 4)。

【问题讨论】:

  • 你找到这个问题的答案了吗?
  • 记不清了,但是看回复,我不这么认为:(

标签: iphone objective-c ipad orientation


【解决方案1】:

不要使用 [[UIDevice currentDevice]orientation] 来检查方向,而是使用状态栏方向来获得准确的方向:- [UIApplication sharedApplication].statusBarOrientation 。我在检查 currentDevice 方向时也遇到了问题,它并不总是返回准确的方向

【讨论】:

  • 你在 shouldAutorotateToInterfaceOrientation 中返回 yes 了吗?
  • 你有没有到这个视图控制器的父控制器,它不会为 shouldAutorotate 返回是。也检查一下
  • 我提到的要记住的一件事是,实际的方向变化可以正常工作。只是当我启动应用程序时,方向错误,我无法修复它。
  • 嘿嘿,可能问题是你处理了setPotraitMode的代码,或者view会出现横屏模式。现在 viewWillAppear 在 shouldAutorate 方法被调用之前被调用。所以当 viewWillAppear 被调用时,最初的默认方向将是 potrait 。尝试在 shouldAutoRotate 或 didRotate 中处理您的代码。这可能会解决您的问题。明白我的意思了吗?并且还要检查状态栏方向的方向,而不是 currentDevice 方向
  • 好主意,但不幸的是没有运气,如果我隐藏状态栏有关系吗?
【解决方案2】:

iPhone 应用程序始终以纵向启动,因为主屏幕仅是纵向的。同样,启动图像旨在以纵向显示。唯一的例外是如果应用程序是纯横向的。

(iPad 应用程序可以在任何屏幕方向启动,并且您必须为两个方向提供单独的启动图像。)

【讨论】:

  • 所以您是说在应用加载后无法旋转视图,而无需手动将手机移回纵向并再次返回横向?
  • 不,我是说你的应用总是以纵向模式启动。这就是为什么设备方向在 viewWillAppear 中是 UIInterfaceOrientationPortrait 的原因。如果不确定,请使用 Notes 应用测试行为。
  • 不,在笔记中,如果我在横向模式下打开它,它会以纵向模式开始,但会翻转为横向模式
  • 您想知道为什么设备方向在 viewWillAppear 中总是 UIInterfaceOrientationPortrait,现在您知道了。如果您还有其他问题,请提出。
  • 不,问题的第一行是I need to make sure that when my UIViewController loads, it rotates as needed.我仍然没有答案
【解决方案3】:

在 viewDidAppear: 中检查方向怎么样?您可以使用“动画”来决定是否使用动画旋转。您的视图可能会出现然后旋转,但我认为这是可以接受的体验。

编辑:作为对我的 cmets 的跟进,这是 Xcode 4 中用于 UIInterfaceOrientation 的屏幕截图:

【讨论】:

  • 只是为了确定——您是否将允许的旋转添加到您的 plist 中?
  • 嗯,没试过,关键是什么?
  • 啊,你的意思是UIInterfaceOrientation,如果我正确阅读了docos是为了初始启动,这不是我想要的,我只是想检测和调整
  • 是的——我的意思是“UIInterfaceOrientation”。我的理解是,这与您的应用程序支持的方向有关,而不是最初的启动。 (见我的帖子编辑)
  • 实际上——我的意思是说“UIInterfaceOrientation”是关于启动和支持的方向。
【解决方案4】:

我正在处理 iOS 应用程序的旋转和方向。我发现如果在viewDidAppear 或之后调用,self.interfaceOrientation 总是会给你一个正确的结果。

在将视图直接添加到UIWindow 时,应首选[UIApplication sharedApplication].statusBarOrientation

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2011-09-10
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多