OGeek|极客世界-中国程序员成长平台

标题: iphone - 以不同于设备方向的方向显示模态视图 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:15
标题: iphone - 以不同于设备方向的方向显示模态视图

我有一个支持所有设备方向的 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 方向时总是恢复为纵向。

为什么?



Best Answer-推荐答案


这种方法做了我需要的一切:

    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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4