iphone - 显示方向与设备方向不同的模态视图

标签 iphone ios orientation modalviewcontroller

我有一个支持所有设备方向的 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/

相关文章:

iphone - 这是 iOS 错误还是可以由开发人员修复?

ios - 使用 ReactiveCocoa 进行身份验证

ipad - 在 iPad 中重新定向期间在 View 中定位对象

Android 最佳实践 : How to let an activity start in any orientation but not allow orientation change

ios - 在 Swift 中如何创建自定义警报 View

android - Android中的相机方向问题

android - Android 开发与 iOS 开发相比如何体验 iOS 开发?

iphone - ZoomingPDFViewer无法在启用分页的UIScrollViewer中缩放

iphone - iOS 默认文档文件

ios - StackView 中 UIPickerView 的问题