I have a customized keyboard created by code, it is a unique input view for a specific textfield. I implement this in my project:
let keyboardContainerView = KBContainerView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height*0.5))
keyboardContainerView.addSubview(myKeyboardView)
textfield.inputView = keyboardContainerView
The KBContainerView is a UIView but has a function, that is when detected device rotation, the frame change. It's quite simple.
override init(frame: CGRect) {
super.init(frame: frame)
NotificationCenter.default.addObserver(self, selector: #selector(updateFrame), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@objc func updateFrame() {
self.frame = CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height*0.5)
}
But I found that though the keyboard container view is changed, as I printed it on the command line but the size of keyboard seems does not change. Here is a gif.
Start from a landscape view does not change the height either.
To fix this problem I tried to use autoresizingMasks
on the input view but things get worse, the keyboard height is wrong when starts the app. The navigation bar also covered keyboard in landscapeview.
In my project, I did not wrote a UIKeyboardviewController
for simplicity. What I am trying to achieve is that when rotating my device, the keyboard can takes half space of the screen. (height is 0.5 * screen height)
Is it because I wrote the change size action in the wrong place?
UIKeyboardViewController
. I assume you meantUIInputViewController
... – Rob