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

这行不通:

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
}

当前回答

转到你的信息。plist和添加两个键:

其他回答

斯威夫特5: 在主视图控制器中,或者主导航控制器中,

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    override var prefersStatusBarHidden: Bool {
        return false
    }

而plist中的“View controller-based status bar appearance”必须为YES,否则上述代码将不会被调用。

如果你想在启动应用程序时隐藏状态栏, plist中的“状态栏初始隐藏”必须为YES。 这可以防止发射图像失真时,额外的蓝色条显示在屏幕顶部。

这对我来说是个问题,因为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)

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

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

和状态栏最初隐藏为YES

iOS 10 / Swift 3.0更新

不再是函数,现在是属性…

override var prefersStatusBarHidden: Bool {
    return true
}

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

open override var prefersStatusBarHidden: Bool { return true }