UIToolbar 隐藏后不显示

Posted

技术标签:

【中文标题】UIToolbar 隐藏后不显示【英文标题】:UIToolbar doesn't show up after it's being hidden 【发布时间】:2012-05-27 13:38:12 【问题描述】:

我有一个 UIButton,它可以显示和隐藏 UIToolbar。

- (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");
        

问题是当我尝试隐藏工具栏时,它工作正常。但是当我再次尝试显示它时,它不会出现。 有任何想法吗?

【问题讨论】:

【参考方案1】:

当您为工具栏的显示设置动画时,您必须在 animations 块中将 alpha 设置为 1.0f

下面是正确的代码;我已经用注释标记了我更改的行。

- (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");
    

【讨论】:

非常感谢!工作! :)

以上是关于UIToolbar 隐藏后不显示的主要内容,如果未能解决你的问题,请参考以下文章

隐藏 UIToolbar UIBarButtonItems

用点击显示/隐藏 UIToolbar

iOS UIToolBar的使用

如何在 iOS 11 和 iPhone X 上实现隐藏 UITabBar 以显示 UIToolbar

隐藏 uitoolbar 在视图和标签栏之间留下空隙

在 Swift 中关闭键盘时,UIToolbar 不再显示在视图中