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

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

问题描述

考虑一个需要在单击按钮时滑出(或隐藏)状态栏的视图控制器.

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

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

我期望根视图扩展到状态栏先前使用的空间(动画,与状态栏动画的持续时间相同).

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

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

蛮力"方法

显然,以下工作...

[[UIApplication sharedApplication] setStatusBarHidden:YESwithAnimation:UIStatusBarAnimationSlide];[UIView animateWithDuration:0.25 动画:^{CGRect 框架 = self.view.frame;frame.origin.y -= 20;frame.size.height += 20;self.view.frame = 框架;}];

...但有缺点:

  • 硬编码幻灯片动画的持续时间
  • 硬编码状态栏的高度
  • 根视图原点停留在 (0,-20).我喜欢我的框架尽可能从 (0,0) 开始.

我已经尝试过的

  • 确保根视图的自动调整大小掩码具有 UIViewAutoresizingFlexibleTopMarginUIViewAutoresizingFlexibleHeight.
  • 隐藏状态栏后调用[self.view setNeedsLayout].
  • 隐藏状态栏后调用[self.view setNeedsDisplay].
  • 在隐藏状态栏之前和之后将 wantsFullScreenLayout 设置为 YES.

解决方案

这很好用,没有任何硬编码.

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

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];
}

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?

(I'm aware there are plenty of similar questions, but I couldn't find any about hiding the status bar on demand as opposed to hiding it to display a new view controller)

The "brute force" approach

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;
}];

...but has disadvantages:

  • Hardcodes the duration of the slide animation
  • Hardcodes the height of the status bar
  • The root view origin stays at (0,-20). I like my frames to start at (0,0) whenever possible.

What I already tried

  • 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天全站免登陆