添加到横向视图时 UIView 旋转了 90 度

发布于 2024-11-29 11:28:10 字数 440 浏览 3 评论 0原文

我正在开发自定义 UIPopoverController。我不是子类化,而是从头开始编写自己的子类作为 NSObject 子类。

它的工作原理非常简单。它向 UIWindow 添加了一个全屏 UIView 来拦截触摸。在该视图之上,它添加了(较小的)弹出框视图,并将弹出框放置在合适的位置。

我已经在 iPad 肖像应用程序中对其进行了测试,效果很好。它很好地定位了弹出窗口视图。

但现在我遇到了一个问题。我需要在横向 iPad 应用程序中使用它,但我的弹出窗口控制器的视图仍然是纵向显示而不是横向显示。它不会自动旋转(我认为它应该自动旋转)。

我知道我可以通过设置转换属性来手动旋转视图。但这会让事情变得非常复杂,因为我必须重新计算正确的定位,同时考虑到 90 度旋转。有什么办法解决这个问题吗?

I'm working on a custom UIPopoverController. I'm not subclassing, I'm writing my own from scratch as a NSObject subclass.

It works quite simply. It adds a fullscreen UIView to the UIWindow to intercept touches. Above that view, it adds the (smaller) popover view and it positions the popover at a suitable point.

I've tested it in a portraid iPad app and it works fine. It positions the popover view well.

But now I've encountered a problem. I need to use it in a landscape iPad app, but the view of my popover controller is still displayed portrait rather than landscape. It doesn't autorotate (which, I thought, it should).

I know I can manually rotate the view by setting the transform property. But this will make things really complex, because I will have to recalculate the correct positioning, taking the 90 degree rotation in account. Is there any way around this?

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

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

发布评论

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

评论(1

浮生未歇 2024-12-06 11:28:10

让我们看看 UIPopoverController 在这里做了什么。从文档中:

如果用户在弹出窗口可见时旋转设备,则弹出窗口
控制器隐藏弹出窗口,然后在末尾再次显示它
旋转。弹出框控制器尝试定位弹出框
适合您,但您可能需要再次呈现或隐藏它
在某些情况下完全。

因此,UIPopoverController 不会像 UIViewController 那样旋转它所维护的弹出窗口视图。这是有道理的,考虑到 UIPopoverController 继承自 NSObject,因此不会继承 UIViewController 处理旋转的代码。

您正在尝试实现自己的UIPopoverController(我不会问为什么!),并且您还继承NSObject,因此您继承的代码不会旋转你对你的看法。当您将全屏 UIView 添加到 UIWindow 时,UIView 存在于 UIWindow 坐标空间内。当您旋转设备时,UIWindow 的坐标空间永远不会改变。为了使您的视图在横向模式下旋转并移动到正确的空间,您必须通过将 CGSAffineTransform 应用于您的视图的 transform 属性来手动执行此操作。 做的事情!)

UIView,也许使用从状态栏的界面方向派生的角度计算旋转矩阵(这是 UIViewController 将为您 自定义UIPopoverController可以管理旋转,但如果可能的话,使用 UIViewController 子类免费获得它会更容易。

Let's have a look at what the UIPopoverController is doing here. From the documentation:

If the user rotates the device while a popover is visible, the popover
controller hides the popover and then shows it again at the end of the
rotation. The popover controller attempts to position the popover
appropriately for you but you may have to present it again or hide it
altogether in some cases.

So the UIPopoverController isn't rotating the popover view that it maintains, like a UIViewController would. This makes sense, considering that UIPopoverController inherits from NSObject, and therefore doesn't inherit the UIViewController's code that handles the rotation.

You're trying to implement your own UIPopoverController (I won't ask why!), and you're also inheriting from NSObject, so you inherit no code that will rotate your views for you. When you add your fullscreen UIView to the UIWindow, the UIView exists within the UIWindow coordinate space. When you rotate the device, the coordinate space of the UIWindow never changes. In order for your view to be rotated and moved to the correct space in landscape mode, you'll have to do this manually by applying a CGSAffineTransform to the transform property of your UIView, perhaps calculating a rotation matrix using an angle derived from the interface orientation of the status bar (this is the sort of thing that a UIViewController would do for you!)

Your custom UIPopoverController could manage the rotation, but it would be easier to get this for free using a UIViewController subclass if possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文