views:

570

answers:

4

Apple apps like Mail, Contacts, and SMS make the UIKeyboard appear as part of the view. The UIKeyboard slides in with the view instead of appearing as a separate animation. I have tried to recreate the same behavior calling [myTextField becomeFirstResponder] in different places like viewWillAppear:animated, viewDidLoad, loadView, navigationController:willShowViewController:animated: but nothing works.

I read in a post (couldn't find the post now) that this seems impossible to do when you want the UIKeyboard to appear with an UITableView. However, this conclusion seems inconsistent with the Apple apps. Can someone confirm?

Is there a solution?

Thanks.

A: 

The keyboard as we iPhone Devs may use it appears in its own UIWindow, so it will always slide up from the bottom, no matter which view-acrobatics you make in your app.

The Apple Apps obviously have access to enhanced functionality of the keyboard which we may not (yet) use.

Pascal
+1  A: 

You can mimic this functionality by resizing your view frames in an animation block when you get a UIKeyboardWillShowNotification notification. Say you have a text field view in the bottom of your view like the SMS app has when it first displays an SMS conversation. When your text field gets a tap, the UIKeyboardWillShowNotification gets triggered at which point you can animate that view like this:

[UIView beginAnimations:nil context:NULL];
[textFieldView setFrame:CGRectMake(0.0f, 416.0f, 320.0f, 216.0f)];
// Also resize your table view
[tableView setFrame:CGRectMake(0.0f, 0.0f, 320.0f, 372.0f)];
[UIView commitAnimations];

You can then reset your view frames if you get a UIKeyboardWillHideNotification notification.

I didn't figure out the exact positions of the frames here, so you'll have to calculate that yourself, but this should give you the basic idea. Just think in terms of where you want your view frames to be after the animations and just set the frames inside an animation block (i.e. -beginAnimations, -commitAnimations).

Best regards.

Matt Long
+1  A: 

Which SDK are you building for? I might be wrong, but I thought Apple said something about improving the behavior of pushing view controllers onscreen and the behavior of other animations.

For what it's worth, I've got this working on my project by calling becomeFirstResponder on my UISearchBar instance in viewWillAppear:. I'm building against the 3.1 SDK.

Jablair
A: 

Jablair's response is close, even going back to the 2.x SDKs -- becomeFirstResponder is the key, but it should be called in viewDidLoad. Here's another (answered) SU question on the topic.

iPhoneDollaraire