Search code examples
objective-cuitoolbar

UIToolbar doesn't show up after it's being hidden


I got a UIButton which makes the UIToolbar show and hide.

- (IBAction)showHideToolbar:(id)sender{
    if (toolBar.hidden == NO) {
        [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^(void){toolBar.alpha =0.0f;}completion:^(BOOL finished){
            toolBar.hidden = YES;}];
        NSLog(@"hides");
    }
    else
        if (toolBar.hidden == YES) {
            [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationCurveEaseIn | UIViewAnimationOptionAllowUserInteraction animations:^(void){toolBar.alpha =0.0f;}completion:^(BOOL finished){
                toolBar.hidden = NO;
            }];
            NSLog(@"show");
        }
}

The problem is that when i try to hide the toolBar,it works fine. But when i try to show it again, it won't show up. Any ideas?


Solution

  • When you animate the showing of the toolbar you have to set the alpha to 1.0f in the animations block.

    Below is the correct code; I've marked the line that I changed with a comment.

    - (IBAction)showHideToolbar:(id)sender {
        if (toolBar.hidden == NO) {
            [UIView animateWithDuration:0.25f 
                                  delay:0.0f 
                                options:UIViewAnimationCurveLinear | UIViewAnimationOptionAllowUserInteraction 
                             animations:^(void){ toolBar.alpha = 0.0f; }
                             completion:^(BOOL finished){ toolBar.hidden = YES; }];
            NSLog(@"hides");
    }
    else
        if (toolBar.hidden == YES) {
            [UIView animateWithDuration:0.25f 
                                  delay:0.0f
                                options:UIViewAnimationCurveEaseIn | UIViewAnimationOptionAllowUserInteraction 
                             animations:^(void){ toolBar.alpha = 1.0f; } // Change from 0.0f to 1.0f
                             completion:^(BOOL finished){ toolBar.hidden = NO; }];
            NSLog(@"show");
        }
    }