优点、问题、向 iOS 应用程序添加另一个 UIWindow 的示例?

发布于 2024-12-17 07:00:25 字数 588 浏览 2 评论 0原文

最近我一直想知道 iOS 应用程序只有一个 UIWindow 这一事实。 创建另一个 UIWindow 并将其放置在屏幕上似乎不是问题。

我的问题有点模糊,但我感兴趣的是:

  • 我可以使用第二个 UIWindow 实现哪些其他方式无法实现的功能?
  • 使用多个 UIWindow 实例时会出现什么问题?
  • 我看到人们使用第二个 UIWindow 在 iPhone 上显示弹出窗口之类的视图。这是一个好方法吗?为什么?为什么不呢?
  • 是否还有其他示例表明拥有另一个 UIWindow 是完全有意义的?

这并不是说我错过了什么。我从来没有觉得有必要创建另一个 UIWindow 实例,但也许它可以做一些我不知道的令人惊奇的事情! :-)

我希望它可以帮助我解决这个问题: 我需要在当前显示的内容上添加“封面视图”。如果已经存在一个或多个模态控制器,它也应该起作用。如果我将 UIView 添加到根控制器的视图中,模态控制器将位于顶部,弹出窗口控制器也是如此。 如果我以模态方式呈现封面视图并且已经存在模态控制器,则仅覆盖部分屏幕。

Recently I've been wondering about the fact that that an iOS app only has one UIWindow.
It does not seem to be an issue to create another UIWindow and place it on screen.

My question is kind of vague, but I'm interested in:

  • What could I potentially achieve with a second UIWindow that cannot be done in other ways?
  • What can go wrong when using multiple UIWindow instances?
  • I have seen that people use a 2nd UIWindow to display popover like views on iPhone. Is this a good way of doing it? Why? Why not?
  • Are there other examples where it is making perfectly sense to have another UIWindow?

It's not that I'm missing something. I have never felt the need to create another UIWindow instance but maybe it would allow doing amazing things I'm not aware of! :-)

I'm hoping that it might help me solve this problem:
I need to add a "cover view" over whatever is currently displayed. It should also work if there are already one or more modal controllers presented. If I add a UIView to the root controller's view, the modal controllers sit on top, so do the popover controllers.
If I present the cover view modally and there is already a modal controller, only part of the screen is covered.

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

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

发布评论

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

评论(3

今天小雨转甜 2024-12-24 07:00:25

从 Rob 的回答开始,我玩了一下,并想为其他试图获取有关此主题的信息的人写下一些注释:

  • 添加另一个 UIWindow 根本不是问题。只需创建一个并makeKeyAndVisible。完毕。
  • 通过使另一个窗口可见来删除它,然后释放不再需要的窗口。
  • 作为“key”的窗口接收所有键盘输入。
  • UIWindow 涵盖了所有内容,甚至包括模态窗口、弹出窗口等。太棒了!
  • UIWindow 始终隐式为纵向。它不旋转。您必须添加一个控制器作为新窗口的根控制器,并让它处理旋转。 (就像主窗口一样)
  • 窗口的级别决定了它显示的“高度”。将其设置为 UIWindowLevelStatusBar 使其覆盖所有内容。将其hidden 属性设置为NO。
  • 第二个 UIWindow 可用于在屏幕上显示浮动在所有内容之上的视图。无需创建虚拟控制器即可将其嵌入 UIPopoverController 中。
  • 它在 iPhone 上特别有用,因为 iPhone 没有弹出控制器,但您可能想模仿类似的东西。
  • 是的,它当然解决了我的问题:如果应用程序退出激活,请在当前显示的任何内容上添加一个覆盖窗口,以防止 iOS 截取应用程序当前内容的屏幕截图。

Starting with Rob's answer I played around a bit and would like to write down some notes for others trying to get information on this topic:

  • It is not a problem at all to add another UIWindow. Just create one and makeKeyAndVisible. Done.
  • Remove it by making another window visible, then release the one you don't need anymore.
  • The window that is "key" receives all the keyboard input.
  • UIWindow covers everything, even modals, popovers, etc. Brilliant!
  • UIWindow is always portrait implicitly. It does no rotate. You'll have to add a controller as the new window's root controller and let that handle rotation. (Just like the main window)
  • The window's level determines how "high" it gets displayed. Set it to UIWindowLevelStatusBar to have it cover everything. Set its hidden property to NO.
  • A 2nd UIWindow can be used to bring views on the screen that float on top of everything. Without creating a dummy controller just to embed that in a UIPopoverController.
  • It can be especially useful on iPhone where there is no popover controller but where you might want to mimic something like it.
  • And yes, it solved of course my problem: if the app resigns activation, add a cover window over whatever is currently shown to prevent iOS from taking a screenshot of your app's current content.
天邊彩虹 2024-12-24 07:00:25

UIWindow 可以浮动在其他 UI 元素(例如系统键盘)上方。

要解决最后一段问题:制作一个与主窗口具有相同框架的 UIWindow 。将其 windowLevel 属性设置为 UIWindowLevelStatusBar。将其hidden 属性设置为NO

A UIWindow can float above other UI elements like the system keyboard.

To address your last paragraph: Make a UIWindow with the same frame as your main window. Set its windowLevel property to UIWindowLevelStatusBar. Set its hidden property to NO.

枯寂 2024-12-24 07:00:25

以下是Apple 的文档,可以帮助您更好地了解 UIWindow:
https://developer.apple.com/库/存档/文档/WindowsViews/Conceptual/WindowAndScreenGuide/WindowScreenRolesinApp/WindowScreenRolesinApp.html

一使用 UIWindow 的多个实例的具体原因是当您需要视频录制应用程序屏幕时。您可能不希望在最终录制的视频中包含某些元素(录制按钮、录制状态等),因此您可以将这些元素放在顶部的单独 UIWindow 中。

事实上,如果您使用 ReplayKit,您将必须为这些排除的 UI 元素使用单独的 UIWindow。更多信息请参见:https://medium.com/ar-tips-and-tricks/how-to-record-a-screen-capture-with-replaykit-whilst-hiding-the-hud-element-bedcca8e31e

Here is Apple's Documentation for better understanding UIWindow:
https://developer.apple.com/library/archive/documentation/WindowsViews/Conceptual/WindowAndScreenGuide/WindowScreenRolesinApp/WindowScreenRolesinApp.html

One good though specific reason to use multiple instances of UIWindow is when you need to video record the app screen. You may not want to include certain elements (recording button, recording status, etc.) in the final recorded video, so you can put those elements in a separate UIWindow on top.

In fact, if you are using ReplayKit, you will have to use a separate UIWindow for these excluded UI elements. More info here: https://medium.com/ar-tips-and-tricks/how-to-record-a-screen-capture-with-replaykit-whilst-hiding-the-hud-element-bedcca8e31e

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