无法在 iPad 上正确旋转翻译后的 UIView

发布于 2024-10-16 05:56:41 字数 1164 浏览 1 评论 0原文

我有一个 UIViewController ,里面有两个 UIView,其布局(在 Interface Builder 中设置)是这样的(洋红色视图是 UIViewController 默认视图):

在此处输入图像描述

并且我将主视图添加到获得此效果的根视图(通过不显示将其 frame 放置在底部):

在此处输入图像描述

现在,当设备旋转时,我得到的是:

在此处输入图像描述

而我仍想隐藏较暗的视图。 这是我用来在它出现时隐藏它的代码(我在 viewDidLoad 方法中使用它):

- (void)hideAll:(float)time{
        CGRect hiddenFrame = CGRectMake(0, 1024, self.view.frame.size.width, self.view.frame.size.height);
        [self.view setFrame:hiddenFrame];
        [self.view setNeedsDisplay];
}

它似乎可以工作,但是当我在旋转时调用它的变体时(在 willRotateToInterfaceOrientation:duration:),什么也没有发生:

    CGRect hiddenFrame = CGRectMake(0, 748, self.view.frame.size.width, self.view.frame.size.height);
    [self.view setFrame:hiddenFrame];
    [self.view setNeedsDisplay];

我错过了什么?我需要在 Interface Builder 中设置一些其他视图属性吗?

更新

在我看来,设备旋转后坐标系不会改变(即在纵向模式下设置在左上角的原点在顺时针旋转后将变为右上角)横向模式)

I have an UIViewController that has two UIViews inside, whose layout (set in Interface Builder) is something like this (the magenta colored view is the UIViewController default view):

enter image description here

and I added the main view to a root view obtaining this effect (by not showing the darker view placing its frame at the bottom):

enter image description here

Now when the device rotates, what I obtain is this:

enter image description here

whereas I'd like to still hide the darker view.
This is the code I use to hide it when it appears (I use it in the viewDidLoad method):

- (void)hideAll:(float)time{
        CGRect hiddenFrame = CGRectMake(0, 1024, self.view.frame.size.width, self.view.frame.size.height);
        [self.view setFrame:hiddenFrame];
        [self.view setNeedsDisplay];
}

and it appears to work, but when I call a variant of it when rotating (in willRotateToInterfaceOrientation:duration:), nothing happens:

    CGRect hiddenFrame = CGRectMake(0, 748, self.view.frame.size.width, self.view.frame.size.height);
    [self.view setFrame:hiddenFrame];
    [self.view setNeedsDisplay];

What am I missing? have I to set some other view property in Interface Builder?

UPDATE

It seems to me that the coordinate system does not change after the device has been rotated (ie the origin set in the upper-left corner in portrait mode will become the upper-right after a clockwise rotation in landscape mode)

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

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

发布评论

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

评论(3

苍风燃霜 2024-10-23 05:56:41

检查 IB 中的支柱和弹簧。也许当您自动旋转时,将您固定在屏幕顶部的支柱会将您的框架移回顶部。

尝试将代码添加到 didRotateToInterfaceOrientation 方法中,以便它可以在旋转发生后运行。

Check your struts and springs in IB. It maybe that when you autorotate, the strut fixing you to the top of the screen moves your frame back to the top.

Try adding your code to the didRotateToInterfaceOrientation method so it can run after rotation has occurred.

只有影子陪我不离不弃 2024-10-23 05:56:41

为什么要更改框架来隐藏它,而不仅仅是

[self.view setHidden:YES];

第二件事是您应该尝试设置“自动调整大小”面板来处理方向更改,而不是通过更改视图的框架。

希望我理解你的意思是正确的。

沙尼

Why to you change the frame to hide it, and not just

[self.view setHidden:YES];

second thing is that you should try to set the "Autoresizing" panel to handle the orientation changes, and not by changing the frame of the view.

hope i understood you right.

shani

挥剑断情 2024-10-23 05:56:41

我遇到的问题可以通过两种方式面对(我希望这可以为将来的某人节省一些时间):

  • 使用 UIViewcontroller (从 xib 加载)并且仅将其视图作为旋转视图,这是我最初尝试解决它。在这种情况下,当您想要隐藏视图时,您必须以编程方式设置视图的框架,并且通过这样做,您必须考虑控制器的坐标系保持不变并且不旋转(即,它始终源自同一个角)
  • 在已经存在的 UIViewcontroller 中使用 UIView (从 xib 加载),这样视图将自动旋转和调整大小,并且您不必在每次旋转时考虑正确的坐标值(这是迄今为止最简单的)方式)

The problem I encountered can be faced in two ways (I hope this can save some time for someone in the future):

  • using an UIViewcontroller (loaded from xib) and only its view as the rotating view, that is my original attempt to solve it. In this case you have to programmatically set the frame for the view when you want to hide it, and by doing so you have to consider that the coordinate system for the controller remains untouched and does not rotate (ie. it is always originated from the same corner)
  • using a UIView (loaded from xib) inside an already existing UIViewcontroller, in this way the view will automatically rotate and resize and you will not have to take the proper coordinates value into account at each rotation (this is by far the simplest way)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文