iphone always returns UIInterfaceOrientationPortrait
I need to make sure that when my UIViewController
loads, it rotates as needed.
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!
I have been reading blogs and even some SO questions, but none of them seem to address the underlying issue.
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.
Any help here is greatly appreciated.
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];
}
EDIT 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.navi开发者_运维百科gationController.interfaceOrientation);
NSLog(@"interface orientation: %d", (int)self.interfaceOrientation);
They all print out "1"
but when I put this code in the willRotateToInterfaceOrientation
method, then they print out the correct int values (1,2,3 or 4).
Instead of using [[UIDevice currentDevice] orientation] for checking orientation use statusbar orientation to get the exact orientation :- [UIApplication sharedApplication].statusBarOrientation . I also had problem while checking currentDevice orientation , it does not always return the exact orientation
iPhone apps are always launched in portrait orientation, because the home screen is portrait-only. Likewise, the launch image is intended to be displayed in portrait orientation. The only exception is if the app is landscape-only.
(iPad apps can be launched in any screen orientation, and and you must provide separate launch images for both orientations.)
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.
EDIT: as a follow up to my comments, here's the screen shot from Xcode 4 for UIInterfaceOrientation:
I was working with the rotations and orientations of the iOS App. I found that self.interfaceOrientation
will always give you a correct result if called in viewDidAppear
or after that.
[UIApplication sharedApplication].statusBarOrientation
should be preferred when adding a view directly to UIWindow
.
Hope it helps.
精彩评论