我有一个支持所有设备方向的 iPhone 应用程序,但我有一个只想横向显示的特定模态视图。
如果设备处于横向方向,则可以正常工作,但如果设备实际处于纵向方向,则它不会按照我想要的方式显示。
由于设备处于纵向方向,因此图像会被裁剪,并且左侧和右侧会超出显示屏的边缘。由于图像仅与窄尺寸一样高,因此顶部和底部显示父 View 的背景。如果我随后将设备旋转为横向,一切都会很好。
在我的研究中,我经常遇到 shouldAutorotateToInterfaceOrientation,但我相信这是一个仅在设备物理旋转时才会触发的事件,这不适用于我的情况。该设备实际上是纵向握持的,但我希望以横向握持的方式显示 View 。
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight] 也被提到,但我在尝试使用它时从未见过它有任何效果。
所以我想我的问题是是否有一种简单的方法可以强制 View 的初始显示方向不同于设备的物理方向,以及具体如何做到这一点。
我使用以下逻辑来模态显示我的 View :
TitleViewController *titleController = [[TitleViewController alloc] initWithNibName:@"TitleViewController" bundle:[NSBundle mainBundle]];
titleController.delegate = self;
titleController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:titleController animated:YES];
[titleController release];
我希望始终以横向方式显示模态视图,即使设备处于纵向状态也是如此。
更新
找到了一个有效的解决方案:
if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
{
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
但是,在处理方向 UIInterfaceOrientationPortraitUpsideDown 时,它最初会以正确的方向显示 View ,但随后将其切换回带有剪裁边的纵向方向,并显示图像上方和下方的背景。
我尝试将角度调整为 270、-90 和其他值,但在 UIInterfaceOrientationPortraitUpsideDown 方向时总是恢复为纵向。
为什么?
最佳答案
这种方法满足了我所需要的一切:
if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
{
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
TitleViewController *titleController = [[TitleViewController alloc] initWithNibName:@"TitleViewController" bundle:[NSBundle mainBundle]];
titleController.delegate = self;
titleController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:titleController animated:YES];
[titleController release];
在呈现我的模态视图之前旋转方向。
我刚刚删除了对 UIInterfaceOrientationPortraitUpsideDown 的支持,因为不需要该方向。
关于iphone - 显示方向与设备方向不同的模态视图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8538430/