如何在iOS 7中使用动画隐藏状态栏?

10
自从iOS 7推出以来,我就不能像在iOS 6中那样使用动画来显示或隐藏状态栏。 目前我使用NSTimer来控制何时隐藏它。
这是我的代码:
- (void)hideStatusBar{
    _isStatusBarHidden=YES;
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
}
- (void)showStatusBar{
_isStatusBarHidden=NO;
[self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
}
    //===================
 _controlVisibilityTimer = [[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(hideStatusBar:) userInfo:nil repeats:NO] retain];

不幸的是,状态栏隐藏的方式似乎有些粗糙,没有淡出效果。是否有人有解决方法?

更新

我解决了隐藏问题,使用了@hahaha的解决方案。我只需要一个视图作为状态栏的背景即可,以下是我的代码。

AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];

self.StatusBarOrange = [[UIView alloc] initWithFrame:CGRectMake(0, 0, appDelegate.window.frame.size.width, 20)];    
[self.StatusBarOrange setBackgroundColor:[UIColor orangeColor]];
[appDelegate.window.rootViewController.view addSubview:self.StatusBarOrange];

现在一切都完美运作!


更新您的最终解决方案并加1!感谢您的贡献! - eric
1个回答

34

你需要打电话

[UIViewController setNeedsStatusBarAppearanceUpdate];

如下示例所示,在动画块内部:

@implementation SomeViewController {
    BOOL _statusBarHidden;
}

- (BOOL)prefersStatusBarHidden {
    return _statusBarHidden;
}

- (void)showStatusBar:(BOOL)show {
 [UIView animateWithDuration:0.3 animations:^{
        _statusBarHidden = !show;
        [self setNeedsStatusBarAppearanceUpdate];
    }];
}

@end

太神奇了你知道这个,谢谢!看起来在iOS5中设置setStatusBarHidden:YES基本上什么都不做。再次感谢。 - Fattie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接