I want to toggle visibility of a toolbar at the top of the screen by sliding it out of the visible area. To that end I store the toolbar's y-coordinate and height on viewDidLoad, and update it accordingly when I want it to fade-in/fade-out.
The relevant code snippet looks something like this (you can also find a sample project at https://github.com/Duffycola/test/tree/master/TestHideToolbar):
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIToolbar* toolbar;
@property (nonatomic, assign) BOOL toolbarVisible;
@property (nonatomic, assign) float toolbarOriginY;
@end
@implementation ViewController
- (void)viewDidLoad;
{
[super viewDidLoad];
self.toolbarVisible = YES;
self.toolbarOriginY = self.toolbar.frame.origin.y;
}
- (IBAction)handleToggleToolbar:(id)sender;
{
self.toolbarVisible = !self.toolbarVisible;
[UIView animateWithDuration:0.5 animations:^{
CGRect toolbarFrame = self.toolbar.frame;
if (self.toolbarVisible)
{
toolbarFrame.origin.y = self.toolbarOriginY;
}
else
{
toolbarFrame.origin.y = self.toolbarOriginY - self.toolbar.frame.size.height;
}
self.toolbar.frame = toolbarFrame;
}];
}
@end
The fade-out works just fine, except when I do anything else on the view. For example when I update a label, the toolbar snaps back to its initial state. I suppose some sort of internal refresh is triggered and the toolbar has an outdated model somewhere.