objective-c 在 iOS 8 应用程序中隐藏状态栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26146012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide Status Bar In iOS 8 app
提问by Aggressor
I have tried
我试过了
[[UIApplication sharedApplication] setStatusBarHidden:YES];
This does nothing.
这没有任何作用。
And I have looked in my Info.plistfile for "View controller-based status bar appearance" but it's not there.
我在我的Info.plist文件中查看了“基于控制器的状态栏外观”,但它不存在。
How can I hide the white status bar at the top of the screen (with the clock and battery charge) inside my app for Xcode 6? Thank you!
如何在我的 Xcode 6 应用程序中隐藏屏幕顶部的白色状态栏(带有时钟和电池电量)?谢谢!
回答by VaporwareWolf
You need to override this method on each view controller unless you have that plist entry.
你需要在每个视图控制器上覆盖这个方法,除非你有那个 plist 条目。
Objective-C
目标-C
-(BOOL)prefersStatusBarHidden{
return YES;
}
Swift 2
斯威夫特 2
override func prefersStatusBarHidden() -> Bool {
return true
}
Swift 3+
斯威夫特 3+
override var prefersStatusBarHidden: Bool {
return true
}
And don't forget to set (if you present a view controller by calling the presentViewController:animated:completion:method):
并且不要忘记设置(如果您通过调用presentViewController:animated:completion:方法呈现视图控制器):
Objective-C
目标-C
vcToBeShownWithoutStatusbar.modalPresentationCapturesStatusBarAppearance = YES;
Swift
迅速
vcToBeShownWithoutStatusbar.modalPresentationCapturesStatusBarAppearance = true
Documentation: https://developer.apple.com/reference/uikit/uiviewcontroller/1621453-modalpresentationcapturesstatusb
文档:https: //developer.apple.com/reference/uikit/uiviewcontroller/1621453-modalpresentationcapturesstatusb
If you change status bar from some container view controller (eg. UINavigationControlleror UIViewControllerwith child view controllers) and you would like to change view controller responsible for status bar you should use childViewControllerForStatusBarHidden:property. Eg:
如果您从某个容器视图控制器(例如UINavigationController或UIViewController使用子视图控制器)更改状态栏,并且您想更改负责状态栏的视图控制器,您应该使用childViewControllerForStatusBarHidden:属性。例如:
Set first view controller instance always responsible for status bar management
设置第一个视图控制器实例始终负责状态栏管理
Objective-C
目标-C
- (UIViewController *)childViewControllerForStatusBarHidden {
return childViewControllers.first; // or viewControllers.first
}
Swift 2
斯威夫特 2
override var childViewControllerForStatusBarHidden() -> UIViewController? {
return childViewControllers.first // or viewControllers.first
}
Swift 3+
斯威夫特 3+
override var childViewControllerForStatusBarHidden: UIViewController? {
return childViewControllers.first // or viewControllers.first
}
Set container view controller responsible for status bar management
设置负责状态栏管理的容器视图控制器
Objective-C
目标-C
- (UIViewController *)childViewControllerForStatusBarHidden {
return nil;
}
Swift 2
斯威夫特 2
override func childViewControllerForStatusBarHidden() -> UIViewController? {
return nil
}
Swift 3+
斯威夫特 3+
override var childViewControllerForStatusBarHidden: UIViewController? {
return nil
}
Documentation: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621451-childviewcontrollerforstatusbarh
文档:https: //developer.apple.com/documentation/uikit/uiviewcontroller/1621451-childviewcontrollerforstatusbarh
回答by nycdanie
- Go to Info.plist file
- Hover on one of those lines and a (+) and (-) button will show up.
- Click the plus button to add new key
- Type in start with capital Vand automatically the first choice will be View controller-based status bar appearance. Add that as the KEY.
- Set the VALUE to "NO"
- Go to you AppDelegate.m for Objective-C (for swift language: AppDelegate.swift)
- Add the code, inside the method
- 转到 Info.plist 文件
- 将鼠标悬停在其中一行上,将显示 (+) 和 (-) 按钮。
- 单击加号按钮添加新密钥
- 输入以大写 V 开头,第一个选项会自动成为View controller-based status bar appearance。将其添加为 KEY。
- 将值设置为“否”
- 转到你的 AppDelegate.m for Objective-C(对于 swift 语言: AppDelegate.swift)
- 在方法内添加代码
For Objective-C:
对于Objective-C:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application setStatusBarHidden:YES];
return YES;
}
For Swift:
对于斯威夫特:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey:Any]?) -> Bool {
application.statusBarHidden = true
return true
}
Done! Run your app and no more status bar!
完毕!运行您的应用程序,不再有状态栏!
回答by CSJordan
You can hide the status bar without writing a single line of code, it just requires two entries into the info.plist the first is
你可以在不写一行代码的情况下隐藏状态栏,它只需要在 info.plist 中输入两个条目,第一个是
"View controller-based status bar appearance" set to NO
“基于视图控制器的状态栏外观”设置为 NO
the second is
第二个是
"Status bar is initially hidden" set to YES
“状态栏最初是隐藏的”设置为“是”
回答by Nerrolken
You can add that row to your Info.plist file if it isn't there. Just go to the project in Xcode, go to the "Info" section, and hover over one of the existing rows. A "+" button should appear, allowing you to add a line and input "View controller-based status bar appearance".
如果该行不存在,您可以将该行添加到您的 Info.plist 文件中。只需转到 Xcode 中的项目,转到“信息”部分,然后将鼠标悬停在现有行之一上。应该会出现一个“+”按钮,允许您添加一行并输入“基于控制器的状态栏外观”。
回答by Ramon Vasconcelos
For iOS 10 with Swift 3 you should use:
对于带有 Swift 3 的 iOS 10,您应该使用:
override var prefersStatusBarHidden: Bool {
get {
return true
}
}
回答by emraz
- Open info.plist
- "View controller-based status bar appearance" set to NO
- "Status bar is initially hidden" set to YES
- Done
- 打开 info.plist
- “基于视图控制器的状态栏外观”设置为 NO
- “状态栏最初是隐藏的”设置为“是”
- 完毕
No need to write a line of code...Cheers
无需写一行代码...干杯
回答by Tester
If you use UIDocumentInteractionController to show data then you never hide status bar so i have alternate of this
如果你使用 UIDocumentInteractionController 来显示数据,那么你永远不会隐藏状态栏,所以我有这个替代
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
this line change the color of status bar content into white
此行将状态栏内容的颜色更改为白色

