ios - Swift iOS 16 我不能再从键盘上显示 UIViewController,而不关闭它

标签 ios swift uiviewcontroller keyboard ios16

在 iOS 16 之前,我可以在键盘上显示一个 UIViewController 而无需关闭它,方法是使用以下代码:

if let window = UIApplication.shared.windows.last, 
    String(describing: type(of: window)).equals("UIRemoteKeyboardWindow") {

    let presentingVC = window.rootViewController

    presentingVC.present(self.myViewController, animated: animated)
}

我尝试在模拟器中运行这段代码,在 iPhone 13 上模拟 iOS 16。

不幸的是,在键盘打开的情况下运行这段代码(和以前一样),“UIRemoteKeyboardWindow”不再出现在窗口中。

我只找到“UITextEffectsWindow”,但从那里显示“myViewController”,它显示在键盘下方。

有没有人遇到过这个问题并且知道如何在不关闭的情况下通过键盘启动 UIViewController?

我需要这段代码主要是因为我有自定义选择器,允许用户输入值来更新 UI,而无需关闭键盘。

更新:

我在装有 iOS 16 的 iPhone 13 模拟器上从当前稳定的 Xcode 版本 (13.4.1) 构建并运行了该应用程序,现在我能够找到 UIRemoteKeyboardWindow。我曾经从 Xcode 版本 14 beta 启动应用程序。由于 Xcode beta 版本,无法找到 UIRemoteKeyboardWindow 是错误吗?

最佳答案

在 iOS16 中 UIRemoteKeyboardWindow 没有出现在 UIApplication 的窗口数组中。

可以作为任何 inputViewControllerparent 访问 UIInputWindowController

然后您可以在 UIInputWindowController 上显示任何 View Controller ,而无需隐藏键盘。

如果您使用自定义输入 View Controller (而不是键盘),您应该观察 UIResponder.keyboardWillChangeFrameNotification 以获取

inputViewController.parent

如果你只使用键盘输入,你应该制作一个假的输入 View Controller ,让你的 View Controller 成为启动时的第一响应者,捕捉 UIResponder.keyboardWillChangeFrameNotification 来存储 UIInputWindowController对象然后退出第一响应者以立即隐藏虚假输入 View 。

代码示例

class SomeViewController: UIViewController {

    private var currentInputViewController: UIInputViewController?
    private var keyboardWindowController: UIViewController?

    override var inputViewController: UIInputViewController?
       isFirstResponder ? currentInputViewController : nil
    }

    init() {
        super.init()

        // some code

        NotificationCenter.default.addObserver(self, selector: #selector(storeKeyboardWindowController), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
    }

    @objc private func storeKeyboardWindowController() {
        if let vc = currentInputViewController?.parent {
            keyboardWindowController = vc
        }
    }

    @objc func inputViewButtonClicked() {
        let vc = AnotherViewControoler()
        keyboardWindowController?.present(vc, animated: true)
    }
}

关于ios - Swift iOS 16 我不能再从键盘上显示 UIViewController,而不关闭它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73142686/

相关文章:

python - 如何使用适用于 Appium 的 Python 客户端在 iOS 应用程序中进行触摸和按住

ios - 使用 SwiftUI 时预览不将设备更改为 iPhone SE

ios - Xcode 在设备连接但不存档时构建

iphone - SDWebImage 未运行

ios - Coreml,验证输入失败。图片无效

ios - 使用带有 GUI 用户请求的 iOS 应用程序连接到 WiFi,而不是以编程方式

ios - 在 Swift 中使用 prepareForSegue 连接 performSegue

ios - UIApplication.sharedApplication().keyWindow?.rootViewController 卡住了我的应用程序

ios - 多个 UIViewController 的单个背景图像

ios - snapshotView AfterScreenUpdates 在 iPhone 上返回空白图像,但在 iPad 上不返回