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

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

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

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

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

OK固定。

使用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个劳动时间。 希望这有一段时间,感谢大家。

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

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

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

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

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

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

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

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

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

 UISupportedInterfaceOrientations      UIInterfaceOrientationPortrait    UIInterfaceOrientationPortraitUpsideDown    UIInterfaceOrientationLandscapeLeft    UIInterfaceOrientationLandscapeRight   
Interesting Posts