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

发布于 2024-12-21 09:38:35 字数 1557 浏览 0 评论 0原文

我有一个支持所有设备方向的 iPhone 应用程序,但我有一个特定的模式视图,我只想以横向显示。

如果设备处于横向方向,则它可以正常工作,但如果设备物理上处于纵向方向,则它不会按照我想要的方式显示。

由于设备以纵向方向握持,因此图像会被裁剪,左侧和右侧会超出显示屏的边缘。由于图像仅与窄尺寸一样高,因此顶部和底部显示父视图的背景。如果我然后将设备旋转为横向,一切都会很好。

在我的研究中,我经常遇到 shouldAutorotateToInterfaceOrientation,但我相信这是一个仅在设备物理旋转时才会触发的事件,这不适用于我的情况。该设备实际上是纵向握持的,但我想以横向握持的方式显示视图。

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight] 也被提到,但当我尝试使用它时,我从未见过它有任何效果。

所以我想我的问题是是否有一种简单的方法可以强制视图的初始显示方向不同于设备的物理方向,以及具体如何做到这一点。

我使用以下逻辑来模态显示我的视图:

    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时,它最初以正确的方向显示视图,但随后将其切换回带有剪裁的纵向方向侧面并显示图像上方和下方的背景。

我尝试将角度调整为 270、-90 和其他值,但在 UIInterfaceOrientationPortraitUpsideDown 方向时总是恢复为纵向。

为什么?

I have an iPhone app that supports all device orientations, but I have a specific modal view that I only want to display in landscape.

If the device is in landscape orientation it works fine, but if the device is physically held in portrait orientation it does not display the way I would like.

Because the device is held in a portrait orientation the image is cropped and the left and right sides are off the edges of the display. Since the image is only as high as the narrow dimension, the top and bottom shows the background of the parent view. If I then rotate the device into a landscape orientation everything is then fine.

In my research I frequently come across shouldAutorotateToInterfaceOrientation, but I believe that is an event that only gets fired when the device is physically rotated which does not apply to my situation. The device is physically held in portrait orientation but I want to display the view as if it was held in landscape.

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight] is also mentioned but I have never seen that have any effect when I've tried using it.

So I guess my question is if there is a simple way to force the initial display of a view in an orientation different than the physical orientation of the device and exactly how to do it.

I am using the following logic to modally display my view:

    TitleViewController *titleController = [[TitleViewController alloc] initWithNibName:@"TitleViewController" bundle:[NSBundle mainBundle]];
    titleController.delegate = self;
    titleController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:titleController animated:YES];
    [titleController release];

I want to always display the modal view in a landscape orientation, even when the device is held in portrait.

UPDATE

Found a solution that works:

    if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
    {
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
        self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
    }

However, when dealing with the orientation UIInterfaceOrientationPortraitUpsideDown, it initially displays the view with the proper orientation but then shifts it back to a portrait orientation with clipped sides and shows the background above and below the image.

I have tried tweaking the angle to 270, -90, and other values but always reverts to portrait when in the UIInterfaceOrientationPortraitUpsideDown orientation.

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何处潇湘 2024-12-28 09:38:35

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

    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 的支持,因为不需要该方向。

This approach did everything I needed:

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

Rotate the orientation before presenting my modal view.

And I just removed support for UIInterfaceOrientationPortraitUpsideDown since that orientation wasn't needed.

习惯成性 2024-12-28 09:38:35

你有没有尝试过:

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

当你要关闭它时,改变为肖像

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

Have you tried :

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

And when you about to dismiss it change to the Portrait

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
毁虫ゝ 2024-12-28 09:38:35

解决方案 1: 要更改初始纵向模式的启动方向,您需要编辑 info.plist 文件。添加“初始界面方向”条目并设置所需的值。

这里是在哪里可以找到 info.plist 文件的屏幕截图。 (如果链接不起作用,请发表评论)


解决方案 2:您还可以在导航控制器中尝试此操作作为窗口的子视图,并且方向通知仅发送到中的第一个子视图“窗户”。

//.m file
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    // Hide status bar
    application.statusBarHidden = YES;
        // --- Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [viewController.view addSubview:navigationController.view];
    [window makeKeyAndVisible];
    return YES;
}

Solution 1: To change the startup orientation from the initial Portrait mode, you need to edit your info.plist file. Add an entry for "intial interface orientation" and set the value that you want.

Here is a screenshot of where to find the info.plist file. (if the link doesn't work, please leave a comment)


Solution 2: You can also try this in your navigation controller as a subview to the window and orientation notifications are only sent to the first subview in "window".

//.m file
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    // Hide status bar
    application.statusBarHidden = YES;
        // --- Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [viewController.view addSubview:navigationController.view];
    [window makeKeyAndVisible];
    return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文