override func preferredStatusBarStyle() -> UIStatusBarStyle {
 return UIStatusBarStyle.LightContent;
}

使用上述代码在任何ViewController中为特定的ViewController设置状态栏颜色为白色,在iOS8中对我来说是行不通的。有什么建议吗?使用UIApplication。shared应用方法,在信息中需要更改后颜色更改。Plist为整个应用程序。

// Change the colour of status bar from black to white
UIApplication.sharedApplication().statusBarStyle = .LightContent

我怎么能改变一些必要的和特定的视图控制器的状态栏颜色?


当前回答

Swift 3.0升级

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        UIApplication.shared.statusBarStyle = .lightContent

        return true
    }

其他回答

点击支持文件组(左边顶部-项目名称)。单击“信息”。点击列表之间的+,就像下面的bundle name。并添加“基于视图控制器的状态栏外观”并将其设置为NO。 然后打开AppDelegate.swift,像这样修改:

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)

return true
}

这是它。

警告


'statusBarStyle'的Setter在iOS 9.0中已弃用:

UIApplication.shared.statusBarStyle = .default

所以我的解决方案是这样的: 从导航控制器做一个扩展:

extension UINavigationController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        if let topViewController = presentedViewController{
            return topViewController.preferredStatusBarStyle
        }
        if let topViewController = viewControllers.last {
            return topViewController.preferredStatusBarStyle
        }

        return .default
    }
}

如果你有一个viewController它会有另一个样式而不是app的样式,你可以做这个

var barStyle = UIStatusBarStyle.lightContent
override var preferredStatusBarStyle: UIStatusBarStyle{
    return barStyle
}

假设你的app状态样式是。default,你希望这个屏幕是。lightcontent 所以barStyle会把。lightContent作为它的默认值,这将改变状态栏的样式为lightContent,然后确保当viewWillDisappear再次改变barStyle为应用程序状态栏的样式,在我们的例子中是。default。

这对我很有用

截至2020年10月,Swift 5, Xcode 12

如果你想把它设置为应用中的所有视图控制器如果你的应用有导航控制器。

你可以在plist文件中这样做,如下所示:

(截至2020年6月10日)

Swift 5(不编辑。plist文件)

如果你正在使用故事板,转到导航控制器,选择导航栏,单击属性检查器,然后更改样式。如果你需要浅色内容(白色状态栏),设置它除了默认设置样式黑色,如果你想要深色内容(黑色状态栏),设置它默认。

默认的(UIBarStyleDefault)导致前景UIStatusBarStyleDefault状态栏为黑色。UIBarStyleBlack会给出一个uistatusbarstyelightcontent状态栏。

编程

let nav = UINavigationController(rootViewController: rootViewController)

    nav.navigationBar.barStyle = .default //gives you dark Content status bar

    nav.navigationBar.barStyle = .black  //gives you light content status bar

没有导航栏(编辑。plist)

添加UIViewControllerBasedStatusBarAppearance /基于视图控制器的状态栏外观到你的信息。Plist, set value为true。

重写控制器中的preferredStatusBarStyle属性

class ViewController: UIViewController {
    override var preferredStatusBarStyle : UIStatusBarStyle {
        return .lightContent
    }
}

我在这个问题上遇到了一些麻烦。我不太喜欢在视图中全局改变状态栏的颜色,然后在视图中改变它,然后像接受的答案一样消失。信不信由你,你可以通过在你想要的视图控制器上重写preferredStatusBarStyle来实现。经过很长一段时间,这是我所做的让它工作:

Change View controller-based status bar appearance in your info.plist to YES. Now any full screen view controller can change the status bar style by overriding preferredStatusBarStyle. I specify full screen because this will not work for (non-full screen) modal view controllers, not without setting modal​Presentation​Captures​Status​Bar​Appearance to Yes that is. Also if you have embedded view controllers, like in a navigation controller for example, it will ask the top most view controller for status bar style. Overriding child​View​Controller​For​Status​Bar​Style and passing the embedded view controller is supposed to work but it didn't for me. So I just returned the embedded view controllers preferred status bar as the preferred status bar style. Something like this: override var preferredStatusBarStyle: UIStatusBarStyle { if let topViewController = viewControllers.last { return topViewController.preferredStatusBarStyle } return .default }