美文网首页
[转] How to set status bar style

[转] How to set status bar style

作者: WMSmile | 来源:发表于2021-06-28 14:36 被阅读0次

    How to set status bar style


    Globally

    If you want to change the status bar style to all of your view controllers, you can do it globally with the help of Info.plist.

    The default color of the status bar is black text.

    Default status bar

    Default status bar

    To change the status bar to white:

    1. Open you Info.plist.
    2. Add View controller-based status bar appearance key (UIViewControllerBasedStatusBarAppearance) and set value to No (false).
    3. Add Status bar style key (UIStatusBarStyle) and set value to Light Content (UIStatusBarStyleLightContent).
    Light Content status bar style

    Light Content status bar style

    Status bar style Value Status bar text color
    Default UIStatusBarStyleDefault Automatically chooses light or dark content based on the user interface style. Black text when traitCollection.userInterfaceStyle = UIUserInterfaceStyle.light and white text when traitCollection.userInterfaceStyle = UIUserInterfaceStyle.dark
    Dark Content UIStatusBarStyleDarkContent Black text, intended for use on light backgrounds
    Light Content UIStatusBarStyleLightContent White text, intended for use on dark backgrounds

    These two flags are from the old days of iOS, and the use case is quite limited. You can just set and forget, there is no way to change this afterward, so if your app has a different status bar style for some view controller, you might have to consider other methods. For a simple app, this might be what you are looking for.

    img Practical Sign in with Apple: Learn everything you need to know about Sign in with Apple to be able to integrate it in your existing app or a new one.

    Style based on view controllers

    By default, the status bar style will respect the view controller instance property, preferredStatusBarStyle. You just need to override preferredStatusBarStyle property in a view controller that you want to change the status bar style to return the style you want.

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    

    The above example will set status bar text color to white.

    preferredStatusBarStyle = .lightContent

    preferredStatusBarStyle = .lightContent

    If your status bar doesn't change according to the value specified in preferredStatusBarStyle, check View controller-based status bar appearance key (UIViewControllerBasedStatusBarAppearance) in your Info.plist and make sure that it set to YES (true). Or you can simply remove the View controller-based status bar appearance key. The default behavior is already using a view controller-based status bar.

    Style based on condition

    If you need to change the status bar style dynamically, e.g., you want it to change based on the scrollable content, you can do that with the same property, preferredStatusBarStyle.

    var preferredStatusBarStyle: UIStatusBarStyle

    Since preferredStatusBarStyle is a get-only property, you can't set the style directly, but you can control it with a help of simple variable and setNeedsStatusBarAppearanceUpdate() method.

    var isDarkContentBackground = false // <1>
    
    func statusBarEnterLightBackground() { // <2>
        isDarkContentBackground = false
        setNeedsStatusBarAppearanceUpdate()
    }
    
    func statusBarEnterDarkBackground() { // <3>
        isDarkContentBackground = true
        setNeedsStatusBarAppearanceUpdate() <4>
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        if isDarkContentBackground { // <5>
            return .lightContent
        } else {
            return .darkContent
        }
    }
    

    <1> We declare a new variable to dictate current background color.
    <2> A function to call when scrollable content under status bar become ligth. We simply set isDarkContentBackground to false.
    <3> A function to call when scrollable content under status bar become dark. We set isDarkContentBackground to true.
    <4> You need to call setNeedsStatusBarAppearanceUpdate() to notify the system that the value from preferredStatusBarStyle has changed.
    <5> We return a status bar style based on isDarkContentBackground value.

    Change status bar style based on variable

    Change status bar style based on variable

    And If you call the setNeedsStatusBarAppearanceUpdate() method within an animation block, the changes will be animated along with the rest of the animation block and produce a nice fading effect.

    func statusBarEnterLightBackground() { // 
        isDarkContentBackground = false
        UIView.animate(withDuration: 0.3) {
            self.setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    func statusBarEnterDarkBackground() { // 
        isDarkContentBackground = true
        UIView.animate(withDuration: 0.3) {
            self.setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    Animated status bar style change

    Animated status bar style change

    原文地址

    相关文章

      网友评论

          本文标题:[转] How to set status bar style

          本文链接:https://www.haomeiwen.com/subject/whrzsltx.html