Swift iOS - 如何在两个 UIWindows 之间切换/切换并制作 keyWindow [英] Swift iOS -How to Switch / Toggle between two UIWindows and make either the keyWindow

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

问题描述

我创建了一个额外的 UIWindow,它将显示在主窗口的顶部.用户按下一个按钮,它会在主窗口上转换.用户可以最小化附加窗口,它会像下面的图片一样位于 tabBar 上方.他们可以放大它以覆盖主窗口或关闭它并被破坏.它工作正常.

I created an additional UIWindow that will get presented on top of the main window. The user presses a button and it transitions over the main window. The user can minimize the additional window and it will sit above the tabBar like it the picture below. They can enlarge it to cover the main window or dismiss it and it gets destroyed. It works fine.

我还创建了一个从屏幕底部启动的自定义操作表.如果附加窗口完全覆盖主窗口并启动操作表,它将在附加窗口内完美启动.如果附加窗口不在屏幕上,它会在主窗口内完美启动.

I also created a custom action sheet that launches from the bottom of the screen. If the additional window completely covers the main window and the action sheet is launched, it will launch perfectly inside the additional window. If the additional window isn't on the screen it launches perfectly inside the main window.

问题是如果附加窗口在屏幕上最小化并且我想从主窗口启动操作表,操作表会在附加窗口内启动,它无法区分两者. 我打开了 3D 可视化工具,它显示主窗口是 off,附加窗口是 on.

The problem is if the additional window is minimized on screen and I want to launch the action sheet from the main window, the action sheet launches inside the additional window instead, it cannot distinguish between the two. I opened up the 3D visualizer and it showed that the main window was off and the additional window was on.

显示自定义操作表时如何区分两个窗口?

How can I distinguish between both windows when displaying the custom action sheet?

顺便说一句,如果两个窗口都存在并且操作表是从主窗口启动的,我会隐藏附加窗口.我还查看了其他答案,他们说要使用我已经在做的 UIApplication.shared.keyWindow.addSubview.

Btw if both windows are present and the action sheet is launched from the main window I hide the additional window. I also looked at other answers and they said to use UIApplication.shared.keyWindow.addSubview which I'm already doing.

CustomActionSheet 类:

CustomActionSheet Class:

var collectionView: UICollectionView!
var deltaY: CGFloat!
let height: CGFloat = 200 // 4 cells x 50 pts each

func displayActionSheet(){

     if let window = UIApplication.shared.keyWindow {

         // collectionView initialized...
         window.addSubview(collectionView)

         deltaY = window.frame.height - height

         collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: height)

         UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [.curveEaseOut], animations: {

             self.collectionView.frame = CGRect(x: 0, y: deltaY, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
         }
      }
}

主视图类:

@IBAction func mainWindowActionSheetButton(_ sender: UIButton) {

      let customActionSheet = CustomActionSheet()
      customActionSheet.displayActionSheet()
}

附加窗口类:

let myVC = MyController()
var nav: UINavigationController?
var window: UIWindow!
var maximized = true

override init() {
    super.init()

    window = UIWindow()
    window.backgroundColor = .clear
    window.windowLevel = UIWindowLevelStatusBar
    nav = UINavigationController(rootViewController: myVC)
    window.rootViewController = nav
    window?.isHidden = false
    window?.makeKeyAndVisible()
 }

 func maximizeOrMinimizeWindow() {

    if maximized {
         // show this full screen
    } else {
        // show this window minimized like in the picture
    }
 }

另一个具有启动操作表的按钮的控制器类:

AnotherController Class that has the button that also launches the action sheet:

@IBAction func additionalWindowActionSheetButton(_ sender: UIButton) {

      let customActionSheet = CustomActionSheet()
      customActionSheet.displayActionSheet()
}

推荐答案

在评论中总结建议.

问题是操作表总是从 key 窗口显示,但附加窗口即使在最小化时仍然是 key 窗口.

The problem is that the action sheet is always shown from the key window but the additional window remains key window even when minimized.

显而易见的解决方案是在最小化附加窗口时使主窗口成为关键窗口.请参阅 UIWindow.makeKey()UIWindow.makeKeyAndVisible().

The obvious solution is to make the main window the key window when the additional one is being minimized. See UIWindow.makeKey() or UIWindow.makeKeyAndVisible().

由于 UIApplication.shared.windows 是按窗口级别排序的(后面的在前),你总是可以使用 UIApplication.shared.windows.first 到达主窗口.

Since UIApplication.shared.windows are ordered by window level (the back one first), you can always reach the main window using UIApplication.shared.windows.first.

因此

UIApplication.shared.windows.first?.makeKey()

将使主窗口成为窗口,最小化的窗口将不再是窗口.

will make the main window the key window and the minimized window will stop being the key window.

这篇关于Swift iOS - 如何在两个 UIWindows 之间切换/切换并制作 keyWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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