优点、问题、在 iOS 应用程序中添加另一个 UIWindow 的例子?

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

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

  • 我可以实现什么潜在的第二个 UIWindow,不能用其他方式?
  • 使用多个 UIWindow实例时会出现什么问题?
  • 我见过人们使用第二个 UIWindow来显示弹出窗口,就像 iPhone 上的视图一样。这样做好吗?为什么?为什么不呢?
  • 有没有其他的例子,它是完全有意义的,有另一个 UIWindow

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

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

16181 次浏览

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.

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.

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