I'm making a custom keyboard that I'll mainly be using outside of the host app. I would need to know when the keyboard appears in floating keyboard mode. For this, according to the documentation, I set a NotificationCenter observer in the KeyboardViewController, but it is not called. where am i stuck?
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow(_:)), name: UIResponder.keyboardDidShowNotification, object: nil)
}
@objc func keyboardDidShow(_ notification: Notification) {
guard let userInfo = notification.userInfo else { return }
guard let screen = notification.object as? UIScreen,
// Get the keyboard’s frame at the end of its animation.
let keyboardFrameEnd = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
// Use that screen to get the coordinate space to convert from.
let fromCoordinateSpace = screen.coordinateSpace
// Get your view's coordinate space.
let toCoordinateSpace: UICoordinateSpace = view
// Convert the keyboard's frame from the screen's coordinate space to your view's coordinate space.
let convertedKeyboardFrameEnd = fromCoordinateSpace.convert(keyboardFrameEnd, to: toCoordinateSpace)
}
The keyboardDidShow function is never called
I found only one solution: The value of view.frame.width
is already available in the viewDidAppear
event. In all previously activated events, this is still 0. So here, if this does not match the width of the UIScreen
, the keyboard is displayed in floating mode.