我想移除屏幕顶部的状态栏。

这行不通:

func application
(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?)
-> Bool
{
        application.statusBarHidden = true
        return true
}

我也试过:

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
}

当前回答

你可以在你的ViewController类范围内使用这段代码

open override var prefersStatusBarHidden: Bool { return true }

其他回答

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        application.isStatusBarHidden = true
        return true
    }

一个对我有效的解决方案;如果你想在加载时隐藏特定视图控制器上的状态栏:

import UIKit

class ViewController: UIViewController {

private var hideStatusBar: Bool = false

override var prefersStatusBarHidden: Bool {
    return hideStatusBar
}

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    return UIStatusBarAnimation.slide
}

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundcolor = .white
    hideStatusBar = true

    UIView.animate(withDuration: 0.3) {
        self.setNeedsStatusBarAppearanceUpdate()
    }
}

注意:如果你在你的信息中设置了“基于控制器的状态栏外观”为“NO”。请列出上面的代码不工作。你应该将键设置为“YES”或者从info.plist中删除它

因此,这里的问题实际上与Swift无关,而是与iOS 7如何处理状态栏外观有关。

默认情况下,当视图控制器在屏幕上时,它们单独控制状态栏的外观。如果你想使用这种方法来控制状态栏,你可以为任何你想修改外观的视图控制器重写以下方法:

prefersStatusBarHidden, preferredStatusBarStyle, preferredStatusBarAnimation,

在你的例子中,你只需要实现prefersStatusBarHidden并返回true。

另一种方法是在应用程序级别控制状态栏的外观。这似乎是你实际上要做的(通过设置application.statusBarHidden)。

为了让这个工作,你需要打开你的应用程序的信息。并添加键UIViewControllerBasedStatusBarAppearance,并给它一个值NO。

更新为iOS 13和Swift 5

如果以上答案都不适合你。检查你的plist,看看你是否有这个:

“基于视图控制器的状态栏外观”

如果是,请确保将其设置为YES!!!!!

然后下面的代码将工作。

override var prefersStatusBarHidden: Bool {
    return true
}

这对我来说是个问题,因为iOS 9不支持上面提到的任何方法,比如UIApplication.sharedApplication()。statusBarHidden = true 或

UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.None)

and

override func prefersStatusBarHidden() -> Bool {
     return true
}

工作,但不提供可编程的解决方案,我可以改变的条件。 (statusBarHidden = true和statusBarHidden = false,就像我们之前做的那样)。

解决这种疯狂的方法:

通过添加prefersStatusBarHidden()像下面这样,你可以通过编程控制隐藏和显示状态栏,而不需要添加UIViewControllerBasedStatusBarAppearance设置到你的info.plist:

var showStatusBar = true

override func prefersStatusBarHidden() -> Bool {
     if showStatusBar {
         return false
     }
     return true
}

private func showStatusBar(enabled: Bool) {
    showStatusBar = enabled
    prefersStatusBarHidden()
}

然后在代码中像这样使用它:

//Hide Status Bar
showStatusBar(false)

OR

//Show Status Bar
showStatusBar(true)