iPhone:在导航过程中强制将方向从纵向转换为横向

3

在导航时,我们有没有办法强制将应用程序方向从纵向改为横向?

我的要求是,在从A推送控制器到B时,B应该是横向的,但我的A控制器是纵向的。


尝试在每个类中使用不同的 -shouldAutorotateToInterfaceOrientation: 方法。这只是一个想法。 - holex
你的应用程序在 UIInterfaceOrientationPortrait 中,你想为单个视图更改界面方向吗? - h4cky
3个回答

9
在你的ViewController-B中,在viewDidLoad方法中添加以下代码行:
  [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

  float   angle = M_PI/2;  //rotate 180°, or 1 π radians
  self.view.layer.transform = CATransform3DMakeRotation(angle, 0, 0.0, 1.0);

如果您使用导航控制器从 A 移动到 B,那么此代码将正常工作。我已经使用了它。希望这可以帮助您。享受编码 :)

0
- (void)viewWillAppear:(BOOL)animated方法中,可以这样说:
    //this is to force the application to re-evaluate device orientation
    //comment the last lines and see
    //cannot use [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];
    //as "setOrientation" is a private method, and we risk to be kicked out by Apple
    UIViewController *c = [[[UIViewController alloc]init] autorelease];
    [self presentModalViewController:c animated:NO];
    [self dismissModalViewControllerAnimated:NO];

...但首先,在您的xib中,将视图的方向设置为所需的方向


0

根据我在上一个应用程序中尝试相同操作的经验,我在Stack Overflow上找到了答案,告诉我不要在基于导航的应用程序中强制更改方向。

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        return NO;
else
    return YES;
}

这个函数可以让您在所需的方向中使用视图控制器,在这种情况下,我已将其设置为横向,并禁止在纵向模式下旋转。

您会面临B视图控制器先加载A视图控制器上的方向的问题。所以我改变了整个应用程序的方向为横向。 希望这有所帮助。


啊,好的,当我尝试过这个时,是在iOS 5上,谢谢你提供的信息 :) - darkmystel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接