Set Orientation to Portrait on iPhone

2019-07-23 05:55发布

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?

3条回答
何必那么认真
2楼-- · 2019-07-23 06:15

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.

查看更多
祖国的老花朵
3楼-- · 2019-07-23 06:21

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.

查看更多
疯言疯语
4楼-- · 2019-07-23 06:28

For previous controller:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
    return YES;
}
return NO;}
查看更多
登录 后发表回答