ios - 在 iOS 7 下,如何动态隐藏和显示状态栏(无论何时)

标签 ios objective-c ios7

假设用户在 View Controller 中并想进入隐藏状态栏的“全屏”类型模式,在 iOS 6 下,这是一个隐藏/显示状态栏的简单方法调用,但无论如何它似乎在 iOS 7 下仍然存在。

我见过这样的解决方案:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

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

在我的 info.plist 中,我将 View controller-based status bar appearance 设置为 NO

我已经无计可施了。我如何隐藏它?

最佳答案

swift 4

显示:

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

隐藏:

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



objective-c

这是一种方法:

在 myViewController.h 中

@interface myViewController : UIViewController {
    BOOL shouldHideStatusBar;
}

然后在myViewController.m中

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

- (BOOL)prefersStatusBarHidden {
    return shouldHideStatusBar;
}

假设当我触摸屏幕时它现在应该显示状态栏。您需要调用:setNeedsStatusBarAppearanceUpdate 来专门让它工作,然后切换(在本例中为 bool)以显示/隐藏。

-(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:

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

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

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


如果您的应用程序也计划与 iOS 6 一起使用,则可能需要查看 this post

关于ios - 在 iOS 7 下,如何动态隐藏和显示状态栏(无论何时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19774968/

相关文章:

opengl-es - iOS 7 和 Cocos2d,glDrawElements 上的 exc_bad_access 完全随机

ios - 关闭自定义 View Controller

ios - 通过 UIButton 将数据传递到另一个 Controller 上的 UITableView

iOS 如何以编程方式模拟 uitextview 上的两次点击?

ios - 在 UITextField 中接受自动更正会产生一个小的闪烁

ios7 - SpriteNode 不遵循 cgpath

ios - 是否可以将 iOS 7 UIViewController 转换(特别是交互)与 subview Controller 一起使用?

ios - 无法使具有标识符 Cell 的单元格出列 - 必须为标识符注册一个 nib 或一个类,或者在 Storyboard中连接一个原型(prototype)单元格?

ios - ImageView 加载本地镜像而不是从 URL

ios - 在 Objective-C 中使用 JSON 数据的 NSJSONSerialization 读取 NSDictionary 集的键值