iPhone SDK:方向(横向和纵向视图)

时间:2009-01-10 15:53:14

标签: cocoa-touch

好的,我的普通应用程序处于纵向模式。我可以强制我的应用程序转到横向模式以获取视图(使用navigationcontroller和viewcontroller),如下所示:

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}

但是当我回到主菜单(tableview)时,它会直接回到肖像。我试试这段代码:

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}

但这不起作用..

有什么想法吗?

5 个答案:

答案 0 :(得分:9)

看一下UIViewController类中的函数shouldAutorotateToInterfaceOrientation:。如果UIView支持方向,则此函数返回YES。如果仅向横向返回YES,则iPhone将自动置于该方向。

以下代码应该这样做。将它放在UIViewController中,该控件控制您想要以横向模式放置的视图。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscape);
}

答案 1 :(得分:7)

为了使其工作,我将其添加到rootviewcontroller:

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

}

这似乎现在正在发挥作用。

答案 2 :(得分:3)

以下是我要做的事情:

首先,将此定义放在文件的顶部,在#imports:

#define degreesToRadian(x) (M_PI * (x) / 180.0)

然后,在viewWillAppear:方法

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];     
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {  
    self.view.transform = CGAffineTransformIdentity;
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
    self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}

如果你想要动画,那么你可以将整个事物包装在一个动画块中,如下所示:

[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];     
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {  
    self.view.transform = CGAffineTransformIdentity;
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
    self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
[UIView commitAnimations];

然后,在您的肖像模式控制器中,您可以执行相反操作 - 检查其当前是否处于横向状态,如果是,则将其旋转回纵向。

答案 3 :(得分:0)

您需要编辑Info.plist文件以添加具有适当值的UIInterfaceOrientation键(UIInterfaceOrientationLandscapeRight或UIInterfaceOrientationLandscapeLeft)。

答案 4 :(得分:0)

您无法使用此私有API。

有一个解决方案:使用视图控制器并将其视图添加到窗口。然后在那个控制器中你强制在shouldAutorotate ...方法中的风景。它工作正常,但要确保你的项目有必要使用它,因为迫使用户转动他的iPhone并不是很聪明。顺便说一下,如果需要,这是一个示例代码。

http://www.geckogeek.fr/iphone-forcer-le-mode-landscape-ou-portrait-en-cours-dexecution.html