SwiftUI:状态栏颜色

SwiftUI:状态栏颜色

问题描述:

是否可以将SwiftUI视图的状态栏更改为白色?

Is there a way to change the status bar to white for a SwiftUI view?

我可能缺少一些简单的东西,但似乎找不到在SwiftUI中将状态栏更改为白色的方法.到目前为止,我只看到.statusBar(hidden: Bool).

I'm probably missing something simple, but I can't seem to find a way to change the status bar to white in SwiftUI. So far I just see .statusBar(hidden: Bool).

与链接的评论一样,我编辑了

As in the comments linked to I edited this question here

但是要回答这个问题并帮助人们直接找到答案:

But to answer this question and help people find the answer directly:

Swift 5和SwiftUI

Swift 5 and SwiftUI

为SwiftUI创建一个名为HostingController.swift的新swift文件

For SwiftUI create a new swift file called HostingController.swift

import SwiftUI

class HostingController<ContentView>: UIHostingController<ContentView> where ContentView : View {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

然后在SceneDelegate.swift中更改以下代码行

Then change the following lines of code in the SceneDelegate.swift

window.rootViewController = UIHostingController(rootView: ContentView())

window.rootViewController = HostingController(rootView: ContentView())