简体   繁体   English

问题pushViewController从Landscape到Portrait

[英]Problem pushViewController from Landscape to Portrait

I am trying to go from a viewcontroller that supports landscape (while in landscape mode), to one that explicitly doesn't (and shouldn't) support landscape. 我试图从支持横向(在横向模式下)的视图控制器转到明确不支持(也不应该)支持横向的视图控制器。 I'm doing this as follows: 我这样做如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

Ideally I want the new viewController that i'm pushing onto the stack to start off initially in portrait, not landscape. 理想情况下,我想要新的viewController,我正在推动堆栈,最初是在纵向,而不是横向开始。 Strangely even with this method implemented, it starts off in Landscape. 奇怪的是,即使实施了这种方法,它也会在Landscape中开始。

My only guess is that Apple doesn't want a user transitioning from landscape to portrait (despite allowing us to go from landscape, back to a previous controller that is in portrait). 我唯一的猜测是Apple不希望用户从横向转换为纵向(尽管允许我们从横向转换回以前的纵向控制器)。

Any insights and/or help would be much appreciated. 任何见解和/或帮助将不胜感激。

I found a way to force portrait. 我找到了强迫肖像的方法。 It's a bit a hack, but here it is. 这有点像黑客,但在这里。 In the -(void)viewDidLoad of the viewController that you want to force portrait for do the following: 在想要强制使用肖像的viewController的 - (void)viewDidLoad中执行以下操作:

UIViewController *viewController = [[UIViewController alloc] init];
[self presentModalViewController:viewController animated:NO];
[self dismissModalViewControllerAnimated:NO];
[viewController release];

This basically forces portrait, by presenting a controller (which only supports portrait by default). 这基本上通过呈现控制器(默认情况下仅支持纵向)强制纵向。

You will need to present your new view controller modally. 您需要以模态方式呈现新的视图控制器。 If your view controller exists within a navigation controller the orientation of all view controllers in the nav stack is implied by the root view controller in the stack. 如果视图控制器存在于导航控制器中,则导航堆栈中所有视图控制器的方向由堆栈中的根视图控制器隐含。 Whatever your root view controller in the nav stack returns from shouldAutoRotateToInterfaceOrientation is then used for all view controllers below it. 无论您的导航堆栈中的根视图控制器从shouldAutoRotateToInterfaceOrientation返回什么,然后用于它下面的所有视图控制器。

The answer by Sahil above is deprecated since iOS 6.0. 上面的Sahil的答案自iOS 6.0以来已被弃用。 However, the following seems to do the same trick: 但是,以下似乎也做了同样的伎俩:

UIViewController *viewController = [[UIViewController alloc] init];
[self presentViewController:viewController animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
[viewController release];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM