SwiftUI dynamically modifies iOS status bar style

Posted by ktstowell on Mon, 14 Feb 2022 15:42:32 +0100

. 1. Set the style of Status in the item setting options page.


-Click item
-Click the application to be set
-Set the style of Status

**Status Bar Style: * * Status Bar Style
-* * Default: * * the Default style is based on the currently applied color mode. Dark Model displays white status bar and Light Model displays black status bar
-* * Dark Content: * * displays the black status bar
-* * Light Content: * * displays a white status bar

**Hide status bar: * * hide status bar

**Attention ♥️: ** When you modify the settings, an item name - item category - Info will appear in the directory Plist file, Info Plist will store some application settings. When you download a project built before Xcode 13.0, you will see Info Plist is a file. Before Xcode 13.0, the content of this file is in the Info column at the top of application - > Project - > project. And some settings in the automatically generated file do not take effect and can be deleted directly.

2. Default settings

In the current state, when we set the style of the status bar and whether to hide the status bar cannot take effect, we need to add attribute settings in info.

According to the steps in the figure, in the Custom iOS Target Properties in Info, move the mouse to an existing option, and a plus sign button will appear. Click Add to add a new record, select or enter view controller based status bar appearance, and the following property is set to NO, Then the initial display status bar and status bar style we set can take effect.

**Attention ♥️: ** If the item we click the plus sign is a drop-down selection, we must fold it down, otherwise we will add the content in this drop-down item.

**Attention ♥️: ** If NO is set, some effects need to be handled manually, such as using When sheet loads a new page, the status bar will change, but it will not switch automatically after we set it. For details, please refer to Sheet chapter content.

3. Dynamic modification


When we switch to a new page or click the full screen of the picture, it may involve dynamically modifying the status bar in the application. At present, we need to use some methods in Swift. For details, please refer to the following code.

struct StatusView: View {
    @State var show = true
    var body: some View {
        VStack(spacing: 16) {
            Button {
                UIApplication.shared.setStatusBarStyle(.darkContent, animated: true)
            } label: {
                Text("Dark Content")
            }
            
            Button {
                UIApplication.shared.setStatusBarStyle(.lightContent, animated: true)
            } label: {
                Text("Light Content")
            }
            
            Button {
                if show {
                    UIApplication.shared.setStatusBarHidden(true, with: .fade)
                } else {
                    UIApplication.shared.setStatusBarHidden(false, with: .fade)
                }
                show.toggle()
            } label: {
                Text(show ? "hide the status bar" : "show the status bar")
            }
        }.onAppear {
            UIApplication.shared.setStatusBarHidden(true, with: .fade)
        }
    }
}

4. Definition method


We can define a separate method to simplify our code. So we can call status () directly darkContent()

class Status {
    // White status bar
    func lightContent() {
        UIApplication.shared.setStatusBarStyle(.lightContent, animated: true)
    }
    
    // Black status bar
    func darkContent() {
        UIApplication.shared.setStatusBarStyle(.darkContent, animated: true)
    }
    
    // Show and hide the status bar
    func toggleStatus(show: Bool, animation: UIStatusBarAnimation = .fade) {
        UIApplication.shared.setStatusBarHidden(show, with: animation)
    }
}

5. SwiftUI hides the status bar

func statusBar(hidden: Bool) -> some View

It cannot be hidden dynamically for the time being. It should be that the Bug will be repaired in a subsequent version, but this problem exists from the beginning. It is unknown what version to repair.

See the app for more information SwiftUI For Beginners , a product designer explains the SwiftUI content from the various processes of self-taught SwiftUI development. Apple App Store searches for SwiftUI For Beginners to download.

-Suitable for designers, Internet practitioners or people who want to make their own applications;
-It is not suitable for technical people. The application is to explain the knowledge needed in each stage from a 0 basic perspective, which is too simple for technical people;
-Content explanation ➕ Direct code effect preview makes the learning process more intuitive;
-Full platform support for iPhone, iPad and Mac, and Apple Watch and TV applications will be launched soon.

Topics: Swift iOS swiftui