6

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);
user1477781
  • 89
  • 1
  • 4
  • did you try to put it under animation block?? – Zaraki Jul 13 '12 at 12:31
  • I tried to put it as an animation block in willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration But the result is the same, the height changes instantly from small to tall, then the animation starts to make it short to wide. – user1477781 Jul 13 '12 at 13:17

2 Answers2

6

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.

eonil
  • 83,476
  • 81
  • 317
  • 516
Harry
  • 141
  • 1
  • 3
2

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.

user1477781
  • 89
  • 1
  • 4
  • In iOS 6 you will not receive UIKeyboardWillShowNotification etc notifications when you have a custom inputView – theraven Feb 03 '13 at 22:55