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

这行不通:

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
}

当前回答

如果你更喜欢可视化的方法而不是编码,请使用以下方法: 在你的info.plist

简单地添加基于视图控制器的状态栏外观为NO

和状态栏最初隐藏为YES

其他回答

在Swift 4.2中,它现在是一个属性。

override var prefersStatusBarHidden: Bool {
    return true
}

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

open override var prefersStatusBarHidden: Bool { return true }

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 }

完成了!运行您的应用程序,没有更多的状态栏!

其实这是我自己发现的。我将添加我的解决方案作为另一种选择。

extension UIViewController {
    func prefersStatusBarHidden() -> Bool {
        return true
    }
}

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

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

prefersStatusBarHidden, preferredStatusBarStyle, preferredStatusBarAnimation,

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

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

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