优点、问题、向 iOS 应用程序添加另一个 UIWindow 的示例?
最近我一直想知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 Rob 的回答开始,我玩了一下,并想为其他试图获取有关此主题的信息的人写下一些注释:
UIWindow
根本不是问题。只需创建一个并makeKeyAndVisible
。完毕。UIWindow
涵盖了所有内容,甚至包括模态窗口、弹出窗口等。太棒了!UIWindow
始终隐式为纵向。它不旋转。您必须添加一个控制器作为新窗口的根控制器,并让它处理旋转。 (就像主窗口一样)hidden
属性设置为NO。UIWindow
可用于在屏幕上显示浮动在所有内容之上的视图。无需创建虚拟控制器即可将其嵌入UIPopoverController
中。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:
UIWindow
. Just create one andmakeKeyAndVisible
. Done.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)UIWindowLevelStatusBar
to have it cover everything. Set itshidden
property to NO.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 aUIPopoverController
.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 itswindowLevel
property toUIWindowLevelStatusBar
. Set itshidden
property toNO
.以下是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