Hiding an UIToolbar with ease-out animation

Friday, January 1, 2010

Here's a little code snippet that someone might find useful for people who want to allow users to hide a toolbar so the full screen can be used to view other content:
static BOOL toolbarHidden = NO;

// toolbar
[UIView beginAnimations:@"toolbar" context:nil];
if (toolbarHidden) {
toolbar.frame = CGRectOffset(toolbar.frame, 0, -toolbar.frame.size.height);
toolbar.alpha = 1;
toolbarHidden = NO;
} else {
toolbar.frame = CGRectOffset(toolbar.frame, 0, +toolbar.frame.size.height);
toolbar.alpha = 0;
toolbarHidden = YES;
}
[UIView commitAnimations];
Obviously the static variable only makes sense if you only have one toolbar. It would be more proper to put that in the class. You could also just use the alpha to determine if it's hidden if you're using that anyway, but I've been playing with different effects and have that commented out half the time.

If you also use setStatusBarHidden:animated: then it seems that the only effect you can get is the alpha fade in and out, so using that somehow on the status bar helps the animations match. You can't change the frame of the status bar.

In case you aren't aware, it's worth noting that setting alpha to 0 will not only visually hide a view but it will also no longer receive events which would appear to make it equivalent to using hidden = YES. Using toolbar.hidden doesn't seem to work with Core Animation.

There are more elaborate examples on this guy's blog.

0 comments:

Post a Comment