问题
I have an application that consists of a login, 3 tables, and then an image.
You can rotate the image to landscape mode but what I want to be able to do is when the 'back' button is pushed and the app returns to the previous screen, I was the app to automatically rotate to give a portrait view.
Is there any way of doing this?
回答1:
If the previous view controller only supports portrait (see shouldAutorotateToInterfaceOrientation:
) then it should automatically rotate.
If the previous view controller supports landscape but you want it to rotate to portrait if it was originally in portrait, you can probably force it by changing what shouldAutorotateToInterfaceOrientation:
returns when navigating back. I wouldn't recommend this; it's inconsistent UI (and it's a bit tedious to figure out what navigation is going on).
There are various ways to set the view controller interface orientation (-[UIDevice setOrientation:]
will attempt to trigger an autorotation), but then you're into the realm of private APIs and potential rejection.
回答2:
For previous controller:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
return YES;
}
return NO;}
回答3:
Override shouldAutorotateToInterfaceOrientation:
in your table view controller and only return the orientation you want.
This might not actually switch back the orientation, so if this alone doesn't work you might have to call setStatusBarOrientation:animated:
on UIApplication
in your table view controller's viewWillAppear:
or viewDidAppear:
methods.
来源:https://stackoverflow.com/questions/3309048/set-orientation-to-portrait-on-iphone