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

这行不通:

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
}

当前回答

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

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中删除它

其他回答

如果你想隐藏和恢复按钮点击的状态栏,而在显示和取消滑动菜单,弹出窗口等,那么你可以使用这个方法:-

隐藏状态栏:-

UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar

恢复状态栏:—

UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal 

在您的项目->通用->部署信息

状态栏风格:——

隐藏状态栏(iOS 10)

 override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true);
    navigationController?.navigationBar.hidden = true // for navigation bar hide
    UIApplication.sharedApplication().statusBarHidden=true; // for status bar hide
}

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

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

iOS 10 / Swift 3.0更新

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

override var prefersStatusBarHidden: Bool {
    return true
}