views:

10078

answers:

7

In several cases I want to add a toolbar to the top of the iPhone keyboard (as in iPhone Safari when you're navigating form elements, for example).

Currently I am specifying the toolbar's rectangle with constants but because other elements of the interface are in flux - toolbars and nav bars at the top of the screen - every time we make a minor interface change, the toolbar goes out of alignment.

Is there a way to programatically determine the position of the keyboard in relation to the current view?

A: 

There's no way (AFAIK) to get the dimensions of the keyboard view. It is however constant, at least in every iPhone version so far.

If you calculate the toolbar position as an offset from the BOTTOM of your view, and take the size of your view into account, then you should not have to worry whether a navbar is present or not.

E.g.

#define KEYBOARD_HEIGHT 240 // example - can't remember the exact size
#define TOOLBAR_HEIGHT 30

toolBarRect.origin.y = viewRect.size.height - KEYBOARD_HEIGHT - TOOLBAR_HEIGHT;

// move toolbar either directly or with an animation

Instead of a define, you could easily create a keyboardHeight function that returns the size based on whether the keyboard is being displayed, and move this toolbar positioning into a separate function that reorganizes your layout.

Also it can depend on where you do this positioning as it's possible the size of your view may change between being loaded and shown based on your navbar setup. I believe the best place to do it would be in viewWillAppear.

Andrew Grant
This worked great, thanks!So far I've been doing this calculation in the selector triggered by UIKeyboardDidShowNotification. I've only tested in a couple of places, but it looks like a good spot.
Rob Drimmie
Yes you can get the size - totally wrong! see below answer
Isaac Waller
+20  A: 

If you register for keyboard notifications, ie UIKeyboardWillShowNotification UIKeyboardWillSHideNotification, etc, the notification you receive will contain the bounds of the keyboard in the userInfo dict (UIKeyboardBoundsUserInfoKey).

See the UIWindow class reference.

amrox
+41  A: 

So basically:

In the init method:

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];

And then have methods referred to above to adjust the position of the bar:

-(void) keyboardWillShow:(NSNotification *) note
{
    CGRect r  = bar.frame, t;
    [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
    r.origin.y -=  t.size.height;
    bar.frame = r;
}

Could make it pretty by animating the position change by wrapping it in

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
//...
    [UIView commitAnimations];
Was poking around my old stuff this morning and noticed that this is a much better and the most comprehensive answer. Thanks!
Rob Drimmie
This answer is still quite relevant over a year later. It helped me get over the hump when developing something related to this.
Cirrostratus
Just a warning to people stumbling across this question now: the UIKeyboardBoundsUserInfoKey is now deprecated in iPhone OS 3.2. There are other, similar ones such as `UIKeyboardFrameBeginUserInfoKey` that give the same information.
Stephen Darlington
There is even a better new way to do that in iOS3.2 the inputAccessoryView property on UITextField and UITextView.
tonklon
This answer helped a ton but is a bit dated. You should use `UIKeyboardFrameEndUserInfoKey` to get the final frame (in screen coordiantes) of the keyboard. You can also use `UIKeyboardAnimationDurationUserInfoKey` and `UIKeyboardAnimationCurveUserInfoKey` to get the rest of the parameters you need to exactly match the behavior of the keyboard.
Dave Peck
A: 

Thanks for the responses. I am using essentially the same technique shown above by Josh, and it looks exactly the same as it would if I'd been able to make the bar a subview of the keyboard (but it requires more code).

This should be a comment. Thanks.
bentford
A: 

the code work tool bar h41 keyboard h 216

can u do the calculation with n if n else so the tool bar follow the keyboard n view position on screen

mark funk
+10  A: 

In 3.0 and above you can get the animation duration and curve from the userInfo dictionary of the notifications.

for instance, to animate the size of the view to make room for the keyboard, register for the UIKeyboardWillShowNotification and do something like the following:

- (void)keyboardWillShow:(NSNotification *)notification
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    [UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];

    CGRect frame = self.view.frame;
    frame.size.height -= [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
    self.view.frame = frame;

    [UIView commitAnimations];
}

Do a similar animation for UIKeyboardWillHideNotification.

David Beck
Looks like a better way to do it on the 3.0 SDK thanks for posting!
Hua-Ying
Thanks for the code. This helps a lot. But when I set my UITextView to become first responder in the viewDidLoad, the UIToolBar doesn't move with the resizing of the self.view. Do you have any idea why?
RyanJM
@RyanJM:becomeFirstResponder and resignFirstResponder have odd behavior when the view is off screen. You should be calling becomeFirstResponder from your viewWillAppear method instead.
David Beck
+6  A: 

As of iOS 3.2 there's a new way to achieve this effect.

UITextFields and UITextViews have an inputAccessory property, which you can set to any view, that is automatically displayed above and animated with the keyboard.

Note that the view you use should neither be in the view hierarchy elsewhere, nor should you add it to some superview, this is done for you.

tonklon
let me try . Though it looks the best way .
hib