如何在iOS中的键盘上方叠加视图

如何在iOS中的键盘上方叠加视图

本文介绍了如何在iOS中的键盘上方叠加视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提供一个覆盖在打开的键盘上的帮助屏幕-帮助屏幕应使下面的整个视图变暗,并保留一个具有完全透明性的小孔,以突出显示"该部件.重点是在突出显示几个视图组件的同时提供一些信息.没有键盘,我只能将视图放在层次结构的顶部,但是在这种情况下,UI使用的键盘带有需要可见的自定义输入附件.

I need to present a help screen that overlays an open keyboard - the help screen should dim the whole view underneath and keep just a small hole with full transparency to "highlight" that piece. The point is to provide some information about several view components while highlighting them. Without a keyboard, I could just put a view at top of the hierarchy, but in this case the UI uses a keyboard with a custom input accessory that needs to be visible.

我试图插入一个新的 UIWindow 并将其放在所有 UIWindow s上方:

I tried to insert a new UIWindow and put it above all the UIWindows:

class CustomTextField: UITextField {
    override var canResignFirstResponder: Bool {
        return false
    }
}

class ViewController: UIViewController {
    var textField: UITextField = CustomTextField()

    override func viewDidAppear(_ animated: Bool) {
        view.backgroundColor = .white
        super.viewDidAppear(animated)

        textField.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
        view.addSubview(textField)
        textField.backgroundColor = UIColor.gray
        textField.becomeFirstResponder()

        DispatchQueue.main.asyncAfter(wallDeadline: .now() + 1) {

            self.window.windowLevel = 100000002.0 // based on experiments with UIApplication.shared.windows this should be the top most window
            let controller = UIViewController()
            controller.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
            self.window.rootViewController = controller
            self.window.makeKeyAndVisible()
        }
    }
    let window = UIWindow(frame: UIScreen.main.bounds)
}

但是这种方法有两个问题:

But there are two problems with this approach:

  1. 一旦窗口变成键并可见,键盘就会隐藏起来.
  2. 即使在使用 windowLevel = 100000002.0 时,键盘似乎也位于窗口上方(键盘已设置动画,因此在隐藏时,我可以看到其位于窗口上方).
  1. The keyboard gets hidden as soon as the window becomes key and visible.
  2. Even when using windowLevel = 100000002.0 it seems that the keyboard is above the window (the keyboard gets animated, so while hiding, I can see that its above my window).

任何想法如何处理这两个问题?甚至有可能吗?

Any ideas how to deal with these two problems? Is it even possible?

推荐答案

好,这是.技巧是将覆盖视图添加到键盘所在的窗口(恰好是 UIApplication.shared.windows.last ):


class ViewController: UIViewController {
    var textField: UITextField = UITextField()

    override func viewDidAppear(_ animated: Bool) {
        view.backgroundColor = .white
        super.viewDidAppear(animated)

        textField.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
        view.addSubview(textField)
        textField.backgroundColor = UIColor.gray
        textField.becomeFirstResponder()

        DispatchQueue.main.asyncAfter(wallDeadline: .now() + 1) {
            // this does the trick
            let customView = UIView(frame: self.view.bounds)
            customView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
            customView.layer.zPosition = CGFloat(Float.greatestFiniteMagnitude)
            UIApplication.shared.windows.last?.addSubview(customView)
        }
    }
}

这篇关于如何在iOS中的键盘上方叠加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!