iphone总是返回UIInterfaceOrientationPortrait [英] iphone always returns UIInterfaceOrientationPortrait
问题描述
我需要确保当我的 UIViewController
加载时,它会根据需要旋转。
I need to make sure that when my UIViewController
loads, it rotates as needed.
我已经实现了 shouldAutorotateToInterfaceOrientation
方法及其全部工作正常,除非应用程序首次加载时(当iphone处于横向模式时)或从UIViewController进入时是纵向模式 interfaceOrientation
和 [[UIDevice currentDevice]方向]
始终返回纵向模式!
I have implemented the shouldAutorotateToInterfaceOrientation
methods and its all working fine, except when the app first loads (when the iphone is in landscape mode) or when going from a UIViewController which was is portrait mode the interfaceOrientation
and the [[UIDevice currentDevice] orientation]
always returns portrait mode!
我一直在阅读博客甚至是一些SO问题,但它们似乎都没有解决潜在的问题。
I have been reading blogs and even some SO questions, but none of them seem to address the underlying issue.
我正在使用标准的UINavigationController应用程序。所以第一个视图是第一个被推入控制器的堆栈。我也尝试过询问self.navigationController的方向,也没有运气。
I am using a standard UINavigationController application. So the first view is the first pushed controller onto the stack. I have also tried asking the self.navigationController its orientation, with no luck either.
非常感谢任何帮助。
编辑:这是我的viewWillAppear代码
EDIT: Here is my code for the 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
以下是我打印当前的方式
Here is how I am printing out the current
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)。
but when I put this code in the willRotateToInterfaceOrientation
method, then they print out the correct int values (1,2,3 or 4).
推荐答案
如何检查viewDidAppear中的方向:相反?您可以使用动画来决定是否使用动画进行旋转。您可以看到视图然后旋转,但我认为这是一种可接受的体验。
What about checking the orientation in viewDidAppear: instead? You can use the 'animated' to decide if you will rotate with animation or not. You view may appear and then rotate, but I think that would an acceptable experience.
编辑:作为我的评论的后续内容,这里是从Xcode 4拍摄的屏幕截图UIInterfaceOrientation:
as a follow up to my comments, here's the screen shot from Xcode 4 for UIInterfaceOrientation:
这篇关于iphone总是返回UIInterfaceOrientationPortrait的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!