I made a custom keyboard view and linked it to the inputView property of a UITextField. Is there a way to change the height of a custom inputView when orientation changes and have the frame change animated smoothly just like the system keyboard? My keyboard size is 768x272 and when the device goes landscape the size becomes 1024x272, but I want to make it bigger like 1024x372. If I change the frame with the code below when I get UIDeviceOrientationDidChangeNotification, the change animation is not smooth.
textField.inputView.frame = CGRectMake(0,0,1024,372);
Per Apple documentation for UIResponder.inputView: "If UIKit encounters an input view with a UIViewAutoresizingFlexibleHeight value in its autoresizing mask, it changes the height to match the keyboard."
So if you want customized height, you shouldn't specify UIVieAutoresizingFlexibleHeight mask.
After many experiments, I found the best answer to my own question. A short answer is change frame when you get UIKeyboardDidHideNotification
.
Custom inputView is embedded in another view controlled by the system called UIPeripheralHostView
.
So changing the custom inputView at wrong time is not reflected immediately or shows an ugly layout at best.
When the device rotates, the system briefly hides the keyboard, then performs a rotation animation of the keyboard from old orientation to new orientation. I think the animation block is inserted somewhere between two notifications UIKeyboardDidHideNotification
and UIKeyboardWillShowNotification
. These notifications are coupled with UIKeyboardWillChangeFrameNotification
. The "frame" in this notification actually means the frame of UIPeripheralHostView
.
So changing the frame of my input view when I get UIKeyboardDidHideNotification
gives the system a chance to adjust the frame of UIPeripheralHostView
before the animation starts, resulting in smooth transition from short keyboard to tall keyboard during orientation change.
This works in iOS 5. But Apple may change the practice in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With