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

这行不通:

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
}

当前回答

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

隐藏状态栏:-

UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar

恢复状态栏:—

UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal 

其他回答

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

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

我使用的是Xcode 8.1 (8B62),部署目标设置为10.1,上面提到的覆盖选项我没有太多的运气。然而,检查部署信息中的“隐藏状态栏”选项对我来说是有用的。

项目>总则

我希望这能有所帮助。

斯威夫特4

//MARK:- Show Status Bar
UIApplication.shared.isStatusBarHidden = false

//MARK:- Hide Status Bar
UIApplication.shared.isStatusBarHidden = true

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

状态栏风格:——

隐藏状态栏(iOS 10)