iOS 如何在键盘上方叠加视图
How to overlay a view above a keyboard in iOS
我需要展示一个覆盖打开的键盘的帮助屏幕 - 帮助屏幕应使下方的整个视图变暗,并只保留一个小孔,使 "highlight" 完全透明。重点是在突出显示它们的同时提供有关多个视图组件的一些信息。没有键盘,我可以将视图放在层次结构的顶部,但在这种情况下,UI 使用带有需要可见的自定义输入附件的键盘。
我试图插入一个新的 UIWindow
并将其放在所有 UIWindow
之上:
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)
}
但是这种方法有两个问题:
- 一旦 window 变为键并可见,键盘就会隐藏。
- 即使在使用
windowLevel = 100000002.0
时,键盘似乎也在 window 上方(键盘动画化,所以在隐藏时,我可以看到它在我的 window 上方)。
有什么办法可以解决这两个问题吗?有可能吗?
好的,正如@Krunal 所指出的,这有点像 的重复。诀窍是将叠加视图添加到键盘所在的 window(恰好是 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)
}
}
}
我需要展示一个覆盖打开的键盘的帮助屏幕 - 帮助屏幕应使下方的整个视图变暗,并只保留一个小孔,使 "highlight" 完全透明。重点是在突出显示它们的同时提供有关多个视图组件的一些信息。没有键盘,我可以将视图放在层次结构的顶部,但在这种情况下,UI 使用带有需要可见的自定义输入附件的键盘。
我试图插入一个新的 UIWindow
并将其放在所有 UIWindow
之上:
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)
}
但是这种方法有两个问题:
- 一旦 window 变为键并可见,键盘就会隐藏。
- 即使在使用
windowLevel = 100000002.0
时,键盘似乎也在 window 上方(键盘动画化,所以在隐藏时,我可以看到它在我的 window 上方)。
有什么办法可以解决这两个问题吗?有可能吗?
好的,正如@Krunal 所指出的,这有点像 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) } } }