ios 状态栏是横向,但 [[UIApplication sharedApplication] statusBarOrientation] 返回纵向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5810397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Status bar is Landscape, but [[UIApplication sharedApplication] statusBarOrientation] returns portrait
提问by Stuart
This problem appears to be intermittent, but I am not sure why exactly. When running my application on the device (iPad), I have some code to load a scroll view with some image views according to the current device orientation. Even though the device is landscape before loading, the views are being loaded as if it were portrait.
这个问题似乎是间歇性的,但我不确定到底是为什么。在设备 (iPad) 上运行我的应用程序时,我有一些代码可以根据当前设备方向加载带有一些图像视图的滚动视图。即使设备在加载之前是横向的,视图也会像纵向一样加载。
The orientation is found by calling [[UIApplication sharedApplication] statusBarOrientation]
.
通过调用找到方向[[UIApplication sharedApplication] statusBarOrientation]
。
The views are set up to adjust their positions when the device is rotated, and indeed, rotating to portrait and then back to landscape returns them to the correct landscape positions. Is it the case that all applications start off in portrait and soon change to landscape if required? Am I trying to check the orientation too soon (during the init
of the first view controller to be loaded)?
视图被设置为在设备旋转时调整它们的位置,实际上,旋转到纵向然后再回到横向将它们返回到正确的横向位置。是否所有应用程序都以纵向模式开始,然后在需要时很快变为横向模式?我是否试图过早地检查方向(在init
要加载的第一个视图控制器期间)?
采纳答案by Greg Combs
If you're subclassing UIWindow or the status bar you'll see this because that's the ipad device's native orientation. The UIWindow translates the orientation and coordinates into what we're used to. After you makeAndKeyVisible, your device and interface orientation in view controllers should be as expected. You wouldn't by chance be using MTStatusBarOverlay would you? I went through the same thing and it came down to the order of instatiation.
如果您继承 UIWindow 或状态栏,您将看到这一点,因为这是 ipad 设备的本机方向。UIWindow 将方向和坐标转换为我们习惯的内容。在 makeAndKeyVisible 之后,视图控制器中的设备和界面方向应该符合预期。你不会偶然使用 MTStatusBarOverlay 吧?我经历了同样的事情,它归结为 instatiation 的顺序。
回答by SubstanceMX
OK Fixed.
确定 固定。
Using UINavigationController, when I popToViewController:animated: from a landscape view to a portrait view, the destination view appears correct but the status bar and also the UIKeyboard keeps the landscape configuration, making a real mess.
使用 UINavigationController,当我 popToViewController:animated: 从横向视图到纵向视图时,目标视图看起来是正确的,但状态栏和 UIKeyboard 保持横向配置,这真是一团糟。
Working aroundAfter thousands of recommendations about statusBarOrientation and references read... https://developer.apple.com/library/content/releasenotes/General/RN-iOSSDK-6_0/index.html
解决方法在阅读了有关 statusBarOrientation 和参考文献的数千条建议之后... https://developer.apple.com/library/content/releasenotes/General/RN-iOSSDK-6_0/index.html
"The setStatusBarOrientation:animated: method is not deprecated outright. It now works only if the supportedInterfaceOrientations method of the top-most full-screen view controller returns 0. This makes the caller responsible for ensuring that the status bar orientation is consistent." (thanks to Vytisin here)
“setStatusBarOrientation:animated: 方法没有被彻底弃用。它现在只有在最顶层全屏视图控制器的 supportedInterfaceOrientations 方法返回 0 时才有效。这使得调用者负责确保状态栏方向是一致的。” (感谢这里的Vytis)
statusBarOrientation only works if supportedInterfaceOrientations returns 0, so... that give us a guess.
statusBarOrientation 只有在 supportedInterfaceOrientations 返回 0 时才有效,所以……这给了我们一个猜测。
If statusBarOrientation is not as expected, one zero return will do it (if always return 0, the view wont rotate, so:
如果 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;
}
Now, in viewDidAppear (believe me, I use this call even when the keyboard notification is recived:
现在,在viewDidAppear(相信我,我用即使在键盘通知recived这一呼吁:
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
more than 48 labor hrs in this. Hope this helps a while, thanks to all.
超过 48 个劳动小时。希望这会有所帮助,谢谢大家。
回答by coolio
Just in case somebody else runs into this, I see a lot of apps that have similar issues in iOS5, actually on iPhone.
万一别人运行到这一点,我看到了很多有在iOS5中类似问题的应用程序,其实在iPhone上。
It might be a bug in iOS 5 or just an interference with a common behavior...
这可能是 iOS 5 中的错误或只是对常见行为的干扰......
I use a custom root view controller class (a UITabBarController subclass, no idea whether that matters) and in that class I've overridden "shouldAutorotateToInterfaceOrientation" to only start rotating after my initial screen setup is done (doing otherwise messed some things up).
我使用一个自定义的根视图控制器类(一个 UITabBarController 子类,不知道这是否重要)并且在那个类中我已经覆盖了“shouldAutorotateToInterfaceOrientation”以在我的初始屏幕设置完成后才开始旋转(否则会搞砸一些事情)。
What I do now is I re-use this class to also set the statusBarOrientation manually to portrait before I allow rotations and to whatever the UI rotates to afterwards.
我现在要做的是重新使用这个类,在允许旋转之前手动将 statusBarOrientation 设置为纵向,以及之后 UI 旋转到的任何位置。
[[UIApplication sharedApplication] setStatusBarOrientation:toInterfaceOrientation animated:YES];
I believe this could fix THIS issue, too, even if the cause might be unrelated.
我相信这也可以解决这个问题,即使原因可能无关。
回答by Mark Krenek
If you are having this issue at launch time, you need to use UIDevice to get the device orientation, since statusBarOrientation will always be portrait until after application:didFinishLaunchingWithOptions:. The only caveat is that you need to enable device orientation notifications prior or asking UIDevice for the orientation.
如果您在启动时遇到此问题,则需要使用 UIDevice 来获取设备方向,因为 statusBarOrientation 将始终为纵向,直到 application:didFinishLaunchingWithOptions: 之后。唯一需要注意的是,您需要先启用设备方向通知或向 UIDevice 询问方向。
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
回答by honcheng
Did you make sure you have all these in info.plist ?
你确定你在 info.plist 中有所有这些吗?
<key>UISupportedInterfaceOrientations</key>
? ? <array>
? ? ? ? <string>UIInterfaceOrientationPortrait</string>
? ? ? ? <string>UIInterfaceOrientationPortraitUpsideDown</string>
? ? ? ? <string>UIInterfaceOrientationLandscapeLeft</string>
? ? ? ? <string>UIInterfaceOrientationLandscapeRight</string>
? ? </array>