在iOS上隐藏状态栏的正确方法,包括动画和调整根视图的大小 [英] Proper way to hide status bar on iOS, with animation and resizing root view

查看:231
本文介绍了在iOS上隐藏状态栏的正确方法,包括动画和调整根视图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个视图控制器,当点击一个按钮时需要滑出(或隐藏)状态栏。

Consider a view controller that needs to slide out (or hide) the status bar when a button is clicked.

- (void) buttonClick:(id)sender
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES
                                            withAnimation:UIStatusBarAnimationSlide];
}

上面有效地隐藏了状态栏,但没有适当调整根视图的大小在顶部留下20像素的间隙。

The above effectively hides the status bar, but does not resize the root view appropriately, leaving a 20 pixel gap on top.

我期望的是根据状态栏先前使用的空间扩展的根视图(动画,具有相同的持续时间比状态栏动画)。

What I expected is the root view to expand over the space that was previously used by the status bar (animated, with the same duration than the status bar animation).

这样做的正确方法是什么?

What's the proper way of doing this?

(我知道有很多类似的问题,但我找不到任何关于按需隐藏状态栏而不是隐藏状态栏以显示新的视图控制器。

显然,以下工作......

Obviously, the following works...

[[UIApplication sharedApplication] setStatusBarHidden:YES
                                        withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:0.25 animations:^{
    CGRect frame = self.view.frame;
    frame.origin.y -= 20;
    frame.size.height += 20;
    self.view.frame = frame;
}];

...但有缺点:


  • 硬编码幻灯片动画的持续时间

  • 硬编码状态栏的高度

  • 根视图原点保留在(0,-20)。我喜欢我的框架尽可能从(0,0)开始。


  • 确保根视图的autoresize掩码具有 UIViewAutoresizingFlexibleTopMargin UIViewAutoresizingFlexibleHeight

  • 隐藏状态栏后调用 [self.view setNeedsLayout]

  • 隐藏状态栏后调用 [self.view setNeedsDisplay]

  • 设置 wantsFullScreenLayout YES

  • Made sure the autoresize mask of the root view has UIViewAutoresizingFlexibleTopMargin and UIViewAutoresizingFlexibleHeight.
  • Called [self.view setNeedsLayout] after hiding the status bar.
  • Called [self.view setNeedsDisplay] after hiding the status bar.
  • Set wantsFullScreenLayout to YES before and after hiding the status bar.

推荐答案

此工作正常,没有任何硬编码

This works fine and has nothing hard coded.

CGRect appFrame = [[UIScreen mainScreen] applicationFrame];

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:0.25 animations:^{
    self.navigationController.navigationBar.frame = self.navigationController.navigationBar.bounds;
    self.view.window.frame = CGRectMake(0, 0, appFrame.size.width, appFrame.size.height);
}];

这篇关于在iOS上隐藏状态栏的正确方法,包括动画和调整根视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆