问题
After reading many posts, I still haven't got a clue how to solve this problem...
The first view of my app is a tableViewController. I override
(BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
and it always returns YES.
If I hold my iPad upright under landscape orientation, it rotates right after I launch the app. However, if I put my iPad flat on the table, even though my homepage is in landscape orientation, it launches in protrait orientation.
So I guess the problem is, how can I get the orientation of my homepage and launch my app with that orientation?
回答1:
I had this same problem. There's some funky stuff that goes on with the first view controller you add to a window and whatever orientation it has. Make sure that the view controllers all return YES for orientations if you want the very first one to open with the right orientation
回答2:
My app is openGL, but the way I handled this was to use notifications:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
// This is called right BEFORE the view is about to rotate. Here I'm using a notification message
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
NSNotification* notification = [NSNotification notificationWithName:@"orientationIsPortrait" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
else {
NSNotification* notification = [NSNotification notificationWithName:@"orientationIsLandscape" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
}
willRotateToInterfaceOrientation is called right BEFORE it actually starts rotating the orientation.
Then in my EAGLView initWithFrame: I set up observers..
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewBecamePortrait:) name:@"orientationIsPortrait" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewBecameLandscape:) name:@"orientationIsLandscape" object:nil];
and in viewBecamePortrait and viewBecameLandscape I handle the changes programatically.
回答3:
On application start willRotateToInterfaceOrientation used only in case if "Home" screen in Landscape mode. This way we can detect orientation when device is "Face Up" or "Face Down". [UIDevice currentDevice].orientation work in case if device not parallel to ground.
回答4:
I used the statusbar orientation to orient my openglview, like this:
UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
NSLog(@"%s: orientation: %i", _cmd, (int)orientation);
来源:https://stackoverflow.com/questions/2859516/ipad-launch-orientation