18

这个问题似乎是间歇性的,但我不确定为什么。在设备(iPad)上运行我的应用程序时,我有一些代码可以根据当前设备方向加载带有一些图像视图的滚动视图。即使设备在加载之前是横向的,但视图被加载时就好像它是纵向的一样。

通过调用找到方向[[UIApplication sharedApplication] statusBarOrientation]

视图被设置为在设备旋转时调整它们的位置,实际上,旋转到纵向然后返回到横向会将它们返回到正确的横向位置。是否所有应用程序都以纵向开始,如果需要,很快就会变为横向?我是否试图过早检查方向(在init要加载的第一个视图控制器期间)?

4

5 回答 5

35

好的 固定的。

使用 UINavigationController,当我 popToViewController:animated: 从横向视图到纵向视图时,目标视图显示正确,但状态栏和 UIKeyboard 保持横向配置,造成真正的混乱。

解决 有关 statusBarOrientation 和参考的数千条建议阅读... https://developer.apple.com/library/content/releasenotes/General/RN-iOSSDK-6_0/index.html

“setStatusBarOrientation:animated: 方法并未完全弃用。它现在仅在最顶层全屏视图控制器的 supportedInterfaceOrientations 方法返回 0 时才有效。这使得调用者负责确保状态栏方向一致。” (感谢Vytis在这里)

statusBarOrientation 仅在 supportedInterfaceOrientations 返回 0 时才有效,所以......这给了我们一个猜测。

如果 statusBarOrientation 不符合预期,则返回一个零(如果始终返回 0,则视图不会旋转,因此:

// if deviceOrientation is A (so I expect statusbarOrientation A
// but statusbarOrientation is B
// return 0
// otherwise 
// return user interface orientation for A

- (NSUInteger)supportedInterfaceOrientations {
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    UIInterfaceOrientation statusBarOrientation =[UIApplication sharedApplication].statusBarOrientation;
    if(deviceOrientation == UIDeviceOrientationPortrait || deviceOrientation == UIDeviceOrientationPortraitUpsideDown){
        if(statusBarOrientation != UIInterfaceOrientationPortrait ||statusBarOrientation != UIInterfaceOrientationPortraitUpsideDown){
             return 0;
        }
    }
    // otherwise
    return UIInterfaceOrientationMaskPortrait;
}

现在,在 viewDidAppear 中(相信我,即使收到键盘通知,我也会使用此调用:

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;

超过 48 个工时。希望这会有所帮助,谢谢大家。

于 2013-01-25T20:47:37.720 回答
7

如果你正在继承 UIWindow 或状态栏,你会看到这个,因为那是 ipad 设备的本机方向。UIWindow 将方向和坐标转换为我们习惯的。在 makeAndKeyVisible 之后,视图控制器中的设备和界面方向应该符合预期。您不会偶然使用 MTStatusBarOverlay 吗?我经历了同样的事情,它归结为instatiation的顺序。

于 2011-05-03T04:07:39.087 回答
4

以防万一其他人遇到这种情况,我看到很多应用程序在 iOS5 中存在类似问题,实际上是在 iPhone 上。

这可能是 iOS 5 中的错误,或者只是对常见行为的干扰......

我使用了一个自定义的根视图控制器类(一个 UITabBarController 子类,不知道这是否重要),并且在那个类中我已经覆盖了“shouldAutorotateToInterfaceOrientation”,仅在我的初始屏幕设置完成后才开始旋转(否则会搞砸一些事情)。

我现在要做的是重新使用这个类,在允许旋转之前手动将 statusBarOrientation 设置为纵向,然后再设置 UI 旋转到的任何位置。

[[UIApplication sharedApplication] setStatusBarOrientation:toInterfaceOrientation animated:YES];

我相信这也可以解决这个问题,即使原因可能无关。

于 2011-08-16T00:12:48.517 回答
1

如果您在启动时遇到此问题,则需要使用 UIDevice 来获取设备方向,因为在 application:didFinishLaunchingWithOptions: 之后,statusBarOrientation 将始终是纵向的。唯一需要注意的是,您需要先启用设备方向通知或向 UIDevice 询问方向。

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
于 2013-03-20T20:04:56.423 回答
0

你确定你在 info.plist 中有所有这些吗?

<key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
于 2011-05-03T01:59:56.103 回答