iPhone SDK: 方向(横屏和竖屏视图)

9

好的,我有一个正常的应用程序,它处于竖屏模式。我可以通过导航控制器和视图控制器强制我的应用程序进入横向模式,如下所示:

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

但是当我回到主菜单(表视图)时,它会立即返回到竖屏模式。我尝试了这段代码:

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

但那并不起作用... 有什么想法吗?
5个回答

9
请看UIViewController类中的shouldAutorotateToInterfaceOrientation:函数。如果您的UIView支持朝向,此函数将返回YES。如果仅返回横向朝向的YES,则iPhone将自动处于该朝向。
以下代码可以实现这一功能。将其放在控制您想要横向模式下的视图的UIViewController中即可。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscape);
}

7
为了使其起作用,我在rootviewcontroller中添加了以下内容:
- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

}

现在似乎可以工作了。


这算使用私有API吗?方向被记录为只读属性。 - lawrence
这行代码只能旋转模拟器。在逻辑上,你不能使用编程脚本让用户旋转他们的iPhone;你只能给用户提示。 - Raptor
1
实际上,Shivan Raptor是错误的。只要将shouldAutoRotateTOInterfaceOrientation设置为仅接受您强制设备更改到的方向,它就会保持在该方向(即使在设备上)。我建议在ViewWillDisappear中撤消此方向更改。 - Craig Warren
2
@Shivan 你是期望手机从用户手中跳出来并旋转到不同的方向吗? - Emil

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];

接下来,在您的纵向模式控制器中,您可以进行反向操作——检查当前是否处于横向模式,如果是,则将其旋转回纵向模式。


你已经接近成功了。self.view.frame = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));self.view.center = CGPointMake(160.0f, 240.0f);我们需要将视图的中心设置为屏幕的中心,并填满屏幕(不包括20像素的空间)。 - Raptor
如果您不想隐藏状态栏,可以添加以下代码:[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; - Iphone_bharat

0

您需要编辑 Info.plist 文件,添加 UIInterfaceOrientation 键和相应的值(UIInterfaceOrientationLandscapeRight 或 UIInterfaceOrientationLandscapeLeft)。


这只是为了回到纵向表格主屏幕吗?因为我想要所有的东西都是纵向的,只有一个视图是横向的。我可以把它变成横向的,但是当我回去时,我必须旋转手机才能把它变回纵向。我不希望用户看到其他的都是横向的。 - Domness

0

您不能使用此私有API。

有一个解决方案:使用视图控制器并将其视图添加到窗口中。然后在该控制器中,在shouldAutorotate...方法中强制横向旋转。它可以正常工作,但请确保您的项目需要使用它,因为强制用户旋转iPhone不是很明智。顺便说一下,如果您需要,这里是一个示例代码。

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


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