Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing height of a custom inputView when device rotates

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);
like image 587
user1477781 Avatar asked Jul 13 '12 06:07

user1477781


2 Answers

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.

like image 117
Harry Avatar answered Nov 28 '22 00:11

Harry


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.

like image 34
user1477781 Avatar answered Nov 28 '22 00:11

user1477781