首页 文章

状态栏为横向,但[[UIApplication sharedApplication] statusBarOrientation]返回纵向

提问于
浏览
15

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

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

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

5 回答

  • 35

    如果你是子类化UIWindow或状态栏,你会看到这个,因为这是ipad设备的原生方向 . UIWindow将方向和坐标转换为我们习惯的方式 . makeAndKeyVisible后,视图控制器中的设备和界面方向应该符合预期 . 你不会偶然使用MTStatusBarOverlay吗?我经历了同样的事情,它归结为实例化的顺序 .

  • 7

    OK Fixed.

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

    Working around 关于statusBarOrientation和引用的成千上万的建议阅读... https://developer.apple.com/library/content/releasenotes/General/RN-iOSSDK-6_0/index.html

    “的setStatusBarOrientation:方法不彻底弃用现在它只有在最顶端的全屏视图控制器返回0的supportedInterfaceOrientations方法这使得负责确保状态栏的方向是一致的来电:动画 . ” (感谢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个劳动时间 . 希望这有一段时间,感谢所有人 .

  • 1

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

    它可能是iOS 5中的一个错误,或者仅仅是对常见行为的干扰......

    我使用自定义的根视图控制器类(一个子类的UITabBarController,不知道是否该事项),并在该类我重写“shouldAutorotateToInterfaceOrientation”只开始我的初始画面设置完成后转动(这样做,否则搞砸一些东西) .

    我现在所做的是,在允许旋转之前,我重新使用这个类来手动将statusBarOrientation设置为portrait,以及之后UI旋转到的任何内容 .

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

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

  • 0

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

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    
  • 4

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

    <key>UISupportedInterfaceOrientations</key>
        <array>
            <string>UIInterfaceOrientationPortrait</string>
            <string>UIInterfaceOrientationPortraitUpsideDown</string>
            <string>UIInterfaceOrientationLandscapeLeft</string>
            <string>UIInterfaceOrientationLandscapeRight</string>
        </array>
    

相关问题