如何在 UIWindows 之间切换? [英] How do I toggle between UIWindows?

查看:48
本文介绍了如何在 UIWindows 之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

我创建了一个我最终想要关闭的覆盖窗口(标签 = 100).但以下代码不起作用:

Scenario:

I've created an overlay window (with tag = 100) that I eventually want to dismiss. But the following code doesn't work:

UIApplication.shared.windows.first?.becomeKey()
UIApplication.shared.windows.last?.resignKey()


(lldb) po UIApplication.shared.windows
▿ 2 elements
  - 0 : <UIWindow: 0x7fa698d0ad00; frame = (0 0; 768 1024); gestureRecognizers = <NSArray: 0x600000048550>; layer = <UIWindowLayer: 0x6000000278a0>>
  - 1 : <UIWindow: 0x7fa698d14bb0; frame = (0 0; 768 1024); autoresize = W+H; tag = 100; gestureRecognizers = <NSArray: 0x600000252a50>; layer = <UIWindowLayer: 0x600000229660>>

有什么想法可以切换或删除覆盖窗口吗?

Any ideas to either toggle or delete the overlay window?

推荐答案

这基本上是一个由两部分组成的问题.

This is essentially a two-part question.

  1. 如何显示创建的 UIWindow 和
  2. 如何关闭它?

我了解到我必须保留参考到新创建的 UIWindow 而不仅仅是具有 local-scope 引用....这是显而易见的.

一旦我有了对辅助 UIWindow 的持久引用,我就可以只为它分配 nil 以将其删除:

I learned that I have to keep a reference to the newly-created UIWindow rather than merely have local-scope reference. ...which is obvious.

Once I have a persistent reference to the ancillary UIWindow, I can merely assign nil to it to remove it:

var hamburgerWindow:UIWindow?

 @IBAction func displayOverlayWindowAction() {
     guard hamburgerWindow != nil else {
         displayOverLay()
         return
     }
     hamburgerWindow = nil
 }

这篇关于如何在 UIWindows 之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆