Sunday, July 11, 2010

Wallpaper Editor: Adding a toolbar with the keyboard

Safari uses a toolbar directly over the keyboard.  I would like to do the same in a generic fashion.  The idea is that when the keyboard is displayed, I'll move a toolbar directly over the top of the keyboard.  I already have a routine that will move a view to expose items over the keyboard, so this is just an extension of this.
One additional characteristic is that I want to have the option of choosing if I move the view to avoid the toolbar, or if I count the toolbar as part of the view.

For now I'm going to assume that the toolbar will only be present when the keyboard is present, and will disappear when it is not present.  This seems like an intuitive behavior, and is how safari acts.

I modify my KeyboardHelper class so that it has an additional properties:
UIToolbar *toolbar                // a toolbar to display (if this is not null).

I then modified my view display and remove so that the toolbar is either removed or displayed.

At the top I calculate the toolbar size, and set an offset.



int toolbarLocation=0;
int toolbarOffset=0;

if (toolbar!=nil)
{
toolbarOffset=toolbar.frame.size.height;
toolbarLocation=myFrame.origin.y+ myFrame.size.height-bounds.size.height-toolbarOffset;
}

Then when moving the views, I also use the toolbarOffset to move them.  It is either zero if no toolbar, or the size of the toolbar.

For the first case of resizing the view, I use it directly.

view.frame=CGRectMake(myFrame.origin.x,myFrame.origin.y,myFrame.size.width,
  myFrame.size.height-bounds.size.height-toolbarOffset);

And for the second case of moving my view, I move it a little extra.

bounds.origin.y = view.window.frame.size.height-bounds.size.height-toolbarOffset;
toolbarLocation=view.window.frame.size.height- bounds.size.height-toolbarOffset;

I set an animation boolean if I'm starting an animation for my resize and then I go ahead and display the toolbar when the keyboard shows up.




if (toolbar!=nil)
{
if (!inAnim)
{
inAnim=true;
[UIView beginAnimations:nil context:view.superview];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.15];
}
toolbar.frame=CGRectMake(toolbar.frame.origin.x,toolbarLocation,toolbar.frame.size.width,toolbar.frame.size.height);
[view.window addSubview:toolbar];
}
if (inAnim)
{
[UIView commitAnimations];
}

Finally in keyboard disappearing, I remove my view if it is present.

Note I really don't have to check for nil, you can send a message to nil, but I think its good form to always check for nil, that way I don't start using a property (which will crash).

- (void)keyboardDisappearing:(NSNotification *)notification {
if (!useCenter)
view.frame=originalSize;
else {
[UIView beginAnimations:nil context:view.superview];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.15];
view.superview.frame=originalSize;
[UIView commitAnimations];
if (toolbar!=nil)
{
[toolbar removeFromSuperview];
}
}

}

No comments:

Post a Comment