如何在 Swift iOS 应用中隐藏状态栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24236912/
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
How do I hide the status bar in a Swift iOS app?
提问by Jay
I'd like to remove the status bar at the top of the screen.
我想删除屏幕顶部的状态栏。
This does not work:
这不起作用:
func application
(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?)
-> Bool
{
application.statusBarHidden = true
return true
}
I've also tried:
我也试过:
func application
(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?)
-> Bool
{
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var controller = UIViewController()
application.statusBarHidden = true
controller.setNeedsStatusBarAppearanceUpdate()
var view = UIView(frame: CGRectMake(0, 0, 320, 568))
view.backgroundColor = UIColor.redColor()
controller.view = view
var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.center = CGPointMake(160, 284)
label.textAlignment = NSTextAlignment.Center
label.text = "Hello World"
controller.view.addSubview(label)
self.window!.rootViewController = controller
self.window!.makeKeyAndVisible()
return true
}
回答by drewag
You really should implement prefersStatusBarHidden on your view controller(s):
你真的应该在你的视图控制器上实现 prefersStatusBarHidden:
Swift 3 and later
Swift 3 及更高版本
override var prefersStatusBarHidden: Bool {
return true
}
回答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 V and 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.swift
Add the code, inside the method
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool { application.statusBarHidden = true return true }
- 转到 Info.plist 文件
- 将鼠标悬停在其中一行上,将显示 (+) 和 (-) 按钮。
- 单击加号按钮以添加新的键键入以大写 V 开头,第一个选项将自动成为基于视图控制器的状态栏外观。
- 将其添加为 KEY。
- 将值设置为“否”
- 去找你 AppDelegate.swift
在方法内添加代码
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool { application.statusBarHidden = true return true }
DONE! Run your app and no more status bar!
完毕!运行您的应用程序,不再有状态栏!
回答by Joseph Mark
Swift 3
斯威夫特 3
In Info.plist
set View controller-based status bar appearance
to NO
在Info.plist
设置View controller-based status bar appearance
为NO
And call UIApplication.shared.isStatusBarHidden = true
并打电话 UIApplication.shared.isStatusBarHidden = true
回答by Vincent Joy
If you want to hide and bring back the status bar on button tap, while at the time of presenting and dismissing slide-in menu, popupsetc, then you can use this method:-
如果您想在按钮点击时隐藏和恢复状态栏,而在呈现和关闭滑入菜单、弹出窗口等时,则可以使用此方法:-
To hide the status bar:-
隐藏状态栏:-
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar
To bring back the status bar:-
要恢复状态栏:-
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal
回答by MiladiuM
if you prefer a visual approach rather than coding it use this method:
in your info.plist
如果您更喜欢视觉方法而不是编码,请使用此方法:在您的 info.plist
simply add
View controller-based status bar appearance
to NO
简单地添加
View controller-based status bar appearance
到NO
and Status bar is initially hidden
as YES
并且Status bar is initially hidden
作为YES
回答by atlwx
Update for iOS 10 / Swift 3.0
iOS 10 / Swift 3.0 更新
No longer a function, now a property...
不再是一个函数,现在是一个属性......
override var prefersStatusBarHidden: Bool {
return true
}
回答by Mohit Tomar
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true);
navigationController?.navigationBar.hidden = true // for navigation bar hide
UIApplication.sharedApplication().statusBarHidden=true; // for status bar hide
}
回答by Samira Ekrami
in Swift 3.x:
在 Swift 3.x 中:
override func viewWillAppear(_ animated: Bool) {
UIApplication.shared.isStatusBarHidden = true
}
回答by Dima
So the issue here actually has nothing to do with Swift but just how status bar appearance is handled as of iOS 7.
所以这里的问题实际上与 Swift 无关,而是从 iOS 7 开始如何处理状态栏外观。
By Default, view controllers individually control the appearance of the status bar when they are on the screen. If you want to use this method of controlling the status bar, you can override the following methods for whatever view controllers you'd like to modify the appearance for:
默认情况下,视图控制器单独控制状态栏在屏幕上的外观。如果你想使用这种控制状态栏的方法,你可以为你想要修改外观的任何视图控制器覆盖以下方法:
prefersStatusBarHidden
,
preferredStatusBarStyle
,
preferredStatusBarAnimation
,
prefersStatusBarHidden
,
preferredStatusBarStyle
,
preferredStatusBarAnimation
,
In your case, you would just implement prefersStatusBarHidden
and return true
.
在您的情况下,您只需实现prefersStatusBarHidden
并返回true
.
The other way would be to control the status bar appearance at the application level. This seems to be what you're actually trying to do (by setting application.statusBarHidden
).
另一种方法是在应用程序级别控制状态栏外观。这似乎是你真正想要做的(通过设置application.statusBarHidden
)。
In order to make this work, you need to open up your app's Info.plist
file and add the key UIViewControllerBasedStatusBarAppearance
, and give it a value of NO
.
为了使这个工作,你需要打开你的应用程序的Info.plist
文件并添加密钥UIViewControllerBasedStatusBarAppearance
,并给它一个值NO
。