是否有方法检查用户当前是否正在使用自定义键盘?我知道如何检查所有的键盘,但我想知道确切的键盘选择。
下面是您如何检查哪些键盘可用,但我如何知道用户当前正在使用哪一个键盘?
class func isKeyboardExtensionEnabled() -> Bool {
guard let keyboards = UserDefaults.standard.dictionaryRepresentation()["AppleKeyboards"] as? [String] else {
return false
}
print("the keyboards are \(keyboards)")
for keyboard in keyboards {
if keyboard.contains("KeyboardExtension"){
return true
}
}
return false
}
为什么我有一个视图需要和键盘一样高。我可以很容易地得到系统的高度,只需让一个文本字段作为响应程序,并在它被看到之前将其删除。
let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue
但是对于uikeyboardwillshow通知高度不正确,导致固定高度视图所需的高度不正确。这是正确的后,2或3调用,但对于自定义键盘,我想知道期望错误的值。
但是对于这个例子来说,如果你想看到不正确的值,这里有一个控制器。
import UIKit
import Foundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(notification:)), name: UIWindow.keyboardWillShowNotification, object: nil)
showKeyboard()
}
func showKeyboard(){
let tf = UITextField(frame: .zero)
self.view.addSubview(tf)
tf.becomeFirstResponder()
}
//MARK: Keyboard
@objc func keyboardWillAppear(notification: NSNotification){
//find the extrasafe bottom
if let userInfo = notification.userInfo as? [String: Any],
let keyboardFrame: NSValue = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue{
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
print("keyboard height is \(keyboardHeight)")
}
}
deinit {
NotificationCenter.default.removeObserver(self, name: UIWindow.keyboardWillShowNotification, object: nil)
}
}
打印输出是
键盘高度为335.0
在iphone x上。如果你安装了gboard并将其设为默认键盘并运行该应用程序,你将得到
键盘高度为75.0
键盘高度为216.0
键盘高度为333.0
. 我只想知道这些垃圾价值。