That's an old question (this thread https://developer.apple.com/forums/thread/16375 dates back iOS9 or this other thread https://stackoverflow.com/questions/67249956/uisearchbar-warning-uitexteffectswindow-should-not-become-key-please-file-a-bu), but I've not found a comprehensive answer yet.
I need to add a Return key to a numeric keyboard (BTW, how is it it does not exist in standard ?)
I used to do it using
let keyBoardWindow = UIApplication.shared.windows.last
to get the keyboard window and then adding the subview (self.returnButton) to it
keyBoardWindow?.addSubview(self.returnButton)
Worked great and still works OK with iOS 15.
But we are told to use connectedScenes instead…
So I adapt code:
var keyBoardWindow: UIWindow? = nil
if #available(iOS 13.0, *) { // use connectedScenes
let scenes = UIApplication.shared.connectedScenes
let windowScene = scenes.first as? UIWindowScene
if let kbWindow = windowScene?.windows.last {
keyBoardWindow = kbWindow
}
} else {
keyBoardWindow = UIApplication.shared.windows.last
}
It runs, but subview does not show.
The problem is that keyboardWindow used to be a UIRemoteKeyboardWindow (the true keyboard window) and now is UITextEffectsWindow, which is not the real keyboardWindow. So adding subview to it adds improperly in the hierarchy.
Note: someone detailed the view hierarchy here: https://developer.apple.com/forums/thread/664547
UIWindow
UITextEffectsWindow
UIInputWindowController
UIInputSetContainerView
UIInputSetHostView
UIEditingOverlayViewController
UIEditingOverlayGestureView
I guess I have to add subview to something else than UITextEffectsWindow (keyBoardWindow), but to what ?
I tried
keyBoardWindow = kbWindow.superview as? UIWindow
to no avail.
I also tried to debug view hierarchy, but keyboard does not show in debugView.
I need to add a Return key to a numeric keyboard (BTW, how is it it does not exist in standard ?)
Any chance you can just do this with an inputAccessoryView
? That’s a supported API and It Just Works™.
I guess I have to add subview to something else than UITextEffectsWindow (keyBoardWindow), but to what ?
I’m sure you know this already, but (for those in the wider audience who may not know) poking around in a private, undocumented view hierarchy is fragile and unsupported. That way madness lies.