在iOS 7下,我如何动态隐藏和显示状态栏(无论何时我想) [英] Under iOS 7, how do I hide and show status bar on the fly (whenever I want to)

查看:210
本文介绍了在iOS 7下,我如何动态隐藏和显示状态栏(无论何时我想)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个用户在View Controller中并希望进入全屏类型模式,状态栏被隐藏,在iOS 6下,这是一个简单的方法调用来隐藏/显示状态栏,但不管是什么它似乎在iOS 7下持续存在。

Say a user is in a View Controller and wants to enter a "full screen" type mode where the status bar is hidden, under iOS 6 it was a simple method call to hide/show the status bar, but no matter what it seems to persist under iOS 7.

我见过这样的解决方案:

I've seen solutions like this:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

但是这不允许在运行时切换它。 (它不接受任何参数。)

But that doesn't allow it to be toggled at runtime. (It doesn't accept any arguments.)

在我的info.plist中我有查看基于控制器的状态栏外观设置为

In my info.plist I have View controller-based status bar appearance set to NO.

我有智慧结束。如何隐藏它?

I'm at wits end. How do I hide it?

推荐答案

Swift 4

Swift 4

显示:

(UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow)?.isHidden = false

hide:

(UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow)?.isHidden = true












Objective-c




Objective-c

这里有一种方法:

在myViewController.h中

in myViewController.h

@interface myViewController : UIViewController {
    BOOL shouldHideStatusBar;
}

然后在myViewController.m

Then in myViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    shouldHideStatusBar = YES;
}

- (BOOL)prefersStatusBarHidden {
    return shouldHideStatusBar;
}

让我们说当我触摸屏幕时它应该现在显示状态栏。您需要调用: setNeedsStatusBarAppearanceUpdate 专门用于显示/隐藏开关(本例中为bool)。

and let's say when I touch the screen it should show the status bar now. You'll need to call: setNeedsStatusBarAppearanceUpdate specifically to get this working and then a switch (bool in this case) to show/hide.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    shouldHideStatusBar = (shouldHideStatusBar)? NO: YES;
    [self setNeedsStatusBarAppearanceUpdate];
}

setNeedsStatusBarAppearanceUpdate


只要视图
控制器的状态栏属性的返回值发生变化,就应该调用它。如果在动画块中从
调用它,那么更改将与动画块的
其余部分一起动画。

This should be called whenever the return values for the view controller's status bar attributes have changed. If it is called from within an animation block, the changes will be animated along with the rest of the animation block.

prefersStatusBarHidden:


返回值布尔值为YES指定状态栏应为
隐藏。默认值为NO。

Return Value A Boolean value of YES specifies the status bar should be hidden. Default value is NO.

讨论如果更改此方法的返回值,请调用
setNeedsStatusBarAppearanceUpdate方法。

Discussion If you change the return value for this method, call the setNeedsStatusBarAppearanceUpdate method.

要指定子视图控制器应控制首选
状态栏隐藏/取消隐藏状态,请实现
childViewControllerForStatusBarHidden方法。

To specify that a child view controller should control preferred status bar hidden/unhidden state, implement the childViewControllerForStatusBarHidden method.






如果您计划使用iOS 6的应用程序,也可以查看这篇文章

这篇关于在iOS 7下,我如何动态隐藏和显示状态栏(无论何时我想)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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