views:

5201

answers:

3

Hi,

I'm working on an iPad app using 3.2 sdk. I'm dealing with obtaining the keyboard size to prevent my textfields from hidding behind it.

I'm getting a Warning in Xcode -> UIKeyboardBoundsUserInfoKey is deprecated what should I use instead not to get this warning?

+11  A: 
Alex Reynolds
Thank you Alex. I was very hard to find information on this topic, your response will surely help in my project.
Mikeware
Do you happen to have a link to that recommendation by Apple?
Senseful
I got that recommendation through a response to a bug report ticket. I don't know where you will find that information publicly, though.
Alex Reynolds
+10  A: 

I played with the previously offered solution but still had issues. Here's what I came up with instead:

    - (void)keyboardWillShow:(NSNotification *)aNotification {
    [self moveTextViewForKeyboard:aNotification up:YES];
}

    - (void)keyboardWillHide:(NSNotification *)aNotification {
        [self moveTextViewForKeyboard:aNotification up:NO]; 
    }

- (void) moveTextViewForKeyboard:(NSNotification*)aNotification up: (BOOL) up{
NSDictionary* userInfo = [aNotification userInfo];

// Get animation info from userInfo
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;

CGRect keyboardEndFrame;

[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];


[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];


// Animate up or down
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];

CGRect newFrame = textView.frame;
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];

newFrame.size.height -= keyboardFrame.size.height * (up? 1 : -1);
textView.frame = newFrame;

[UIView commitAnimations];
}
Jason
As to Thomas's comment below see the documentation on setAnimationsBeginsFromCurrentState:"This method does nothing if an animation is not in flight or invoked outside of an animation block. The default value is NO."So unless you are doing something outside the normal scope of this animation, you don't need to add it to the code. Every time I've used this it animates just fine.
Jason
You guys saved my day. Thank u!
f0rz
While this code will generally work, there are some breaking conditions. For example, if the user has more than one keyboard installed and they switch keyboards using the international keyboard key (which only displays if you have more than one keyboard installed). This will throw a UIKeyboardWillShowNotification, even though the keyboard is already shown. The result will be that your textField will be moved up another keyboard height up the screen. This will occur every time the international keyboard key is pressed.
Harkonian
I agree with Harkonian that you will need to set a BOOL that keeps track of whether there is already a keyboard being displayed. Before UIKeyboardBoundsUserInfoKey, I had offered a solution that's very similar to Jason's solution with the addition of the BOOL that kept track of the presence of the keyboard. I do like Jason's solution though. Very clean. http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present/2703756#2703756
Shiun
+1  A: 

@Jason, you code if fine except for one point.

At the moment you are not actually animating anything and the view will simply `pop' to its new size.height.

You have to specify a state from which to animate. An animation is a sort of (from state)->(to state) thing.

Luckily there is a very convenient method to specify the current state of the view as the (from state).

[UIView setAnimationBeginsFromCurrentState:YES];

If you add that line right after beginAnimations:context: your code works perfectly.

Thomas