同时隐藏状态栏和导航栏,如图片应用程序中

时间:2013-03-05 04:38:19

标签: iphone ios animation uinavigationbar statusbar

我有一个包含大量文字的视图,所以我希望允许用户在单击时隐藏statusBar + navigationBar。我真的很喜欢图片应用程序中的隐藏风格,其中statusBar和navigationBar隐藏在一起(不滑动,只是淡出),有一些animationDuration,所以我尝试做类似的事情。这是我在touchesDidBegan方法中所做的:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    [UIView setAnimationDuration:0.5];
[UIView beginAnimations:@"" context:nil];
    [[UIApplication sharedApplication] setStatusBarHidden:!([UIApplication sharedApplication].statusBarHidden) withAnimation:UIStatusBarAnimationNone];
    [self.navigationController setNavigationBarHidden:(!self.navigationController.navigationBarHidden) animated:NO];
    [UIView commitAnimations];
    self.navigationController.navigationBar.translucent = !self.navigationController.navigationBar.translucent; // this is needed to make bars appear on top of my view.
}

但是这并没有同时隐藏条形图。它让它们向下滑动。它与上述方法的版本具有相同的效果:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    // deleted UIView animation, changed animation type to "slide"
    [[UIApplication sharedApplication] setStatusBarHidden:!([UIApplication sharedApplication].statusBarHidden) withAnimation:UIStatusBarAnimationSlide];
    // enabled animation for navBar
    [self.navigationController setNavigationBarHidden:(!self.navigationController.navigationBarHidden) animated:YES];
    self.navigationController.navigationBar.translucent = !self.navigationController.navigationBar.translucent; // this is needed to make bars appear on top of my view.
}

如果我摆脱UIView动画并隐藏没有动画的条形图,它们会隐藏并同时显示,但速度很快。 也许我走错了方向。如果有人可以帮我解决这个问题,我将不胜感激。

编辑:让它工作

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // don't forget to set navigationBar.translucent to YES
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    [UINavigationBar setAnimationDuration:3.0];

    [UINavigationBar beginAnimations:@"" context:nil];
    [[UIApplication sharedApplication] setStatusBarHidden:!([UIApplication sharedApplication].statusBarHidden) withAnimation:NO];
    if ([UIApplication sharedApplication].isStatusBarHidden)
        [self.navigationController.navigationBar setAlpha:0.0];
    else [self.navigationController.navigationBar setAlpha:1.0];
    [UINavigationBar commitAnimations];
}

3 个答案:

答案 0 :(得分:4)

对于带动画的隐藏UIStatusBar

[[UIApplication sharedApplication] setStatusBarHidden: YES withAnimation: UIStatusBarAnimationSlide];

对于带动画的隐藏UINavigationBar

[UINavigationBar beginAnimations:@"NavBarFade" context:nil];
self.navigationController.navigationBar.alpha = 1;
[self.navigationController setNavigationBarHidden:YES animated:NO]; //Animated must be NO!
[UINavigationBar setAnimationCurve:UIViewAnimationCurveEaseIn]; 
[UINavigationBar setAnimationDuration:1.5];
self.navigationController.navigationBar.alpha = 0;
[UINavigationBar commitAnimations];

答案 1 :(得分:3)

在此查看https://github.com/kirbyt/KTPhotoBrowser的此演示,您会发现如何隐藏和显示状态栏和导航栏。

  • 大多数情况下,您需要使用NSTimer在4或5秒Hidding状态栏或导航栏后设置自动隐藏

  • 您还可以使用Touch Begun标记此计时器以随机显示或隐藏。

希望能帮助你完成任务。使用Barack点检查上面Github链接的Sample Demo。你可以轻松找到隐藏和显示Statusbar或NavigationBar的功能。

答案 2 :(得分:0)

- (void)toggleStatusBarAndNavBar:(BOOL)hidden {

UINavigationBar *navBar = self.navigationController.navigationBar;
[[UIApplication sharedApplication] setStatusBarHidden:hidden withAnimation:UIStatusBarAnimationSlide];

[UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    // 先显示navigationBar
    if (!hidden)
    {
        [self.navigationController setNavigationBarHidden:hidden animated:NO];
    }

    navBar.frame = CGRectMake(navBar.frame.origin.x,
                              hidden ? -navBar.frame.size.height : 20,
                              navBar.frame.size.width,
                              navBar.frame.size.height);
} completion:^(BOOL finished) {
    if (hidden)
    {
        [self.navigationController setNavigationBarHidden:hidden animated:NO];
    }
}]; }

它将同时隐藏状态栏和导航栏。为状态栏的动画样式选择 UIStatusBarAnimationSlide 。并更改navBar的框架。

  1. 隐藏时,隐藏等于。我们应该首先更改navBar的框架,然后隐藏navBar。
  2. 显示时,隐藏等于 YES 。我们首先制作navBar visual,然后更改框架。
  3. 我们选择 UIViewAnimationOptionCurveEaseOut ,它看起来会更好。