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

查看:15
本文介绍了在 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 中,我将 基于控制器的状态栏外观 设置为 NO.

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

显示:

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

隐藏:

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

<小时><小时><小时>

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