简体   繁体   English

如何更改 SwiftUI 中的状态栏背景颜色?

[英]How to change the status bar background color in SwiftUI?

I have a ZStack which has Color.orange set on it:我有一个ZStack ,上面设置了Color.orange

struct HomeView: View {
    init() {
        UITabBar.appearance().barTintColor = UIColor.orange
    }
    var body: some View {
        ZStack {
            Color.orange
            TabView {
                Settings()
                .tabItem {
                    Image(systemName: "gear")
                    Text("Settings")
                }
                MapKitView()
                .tabItem {
                    Image(systemName: "location.circle.fill")
                    Text("Home")
                }
                ProfileView()
                .tabItem {
                    Image(systemName: "person")
                    Text("Profile")
                }
            }
            .font(.headline)
        }
    }
}

and in this ZStack I have a TabView with child views that all have orange ZStacks .在这个ZStack中,我有一个TabView ,其中的子视图都有橙色ZStacks However these child views, including Settings() and MapKitView() shown below, do not have an orange status bar.然而,这些子视图,包括如下所示的Settings()MapKitView() ,没有橙色状态栏。

Settings()设置()

struct Settings: View {

    var body: some View {
        ZStack {
            Color.orange
            VStack {
                NavigationView {
                       ...
                    }
            }
        }
    }
}

MapKitView() MapKitView()

struct MapKitView: UIViewRepresentable {
    func makeUIView(context: Context) -> MKMapView {
        let mapView = MKMapView()
        return mapView
    }
    func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<MapKitView>) {

    }
}

在此处输入图像描述

How can I make the status bar orange across all my views?如何在我的所有视图中使状态栏变为橙色?

ZStack {
...
}
.edgesIgnoringSafeArea(.vertical) // or .top

Assuming that the UITabBarController contains UIHostingController instances, you can fix this, by setting the view background color on the UIHostingController .假设UITabBarController包含UIHostingController实例,您可以通过在UIHostingController上设置视图背景颜色来解决此问题。

viewControllers?.forEach({ (vc) in
    // Set `UIHostingViewController` backgrounds to color the statusbar area.
    vc.view.backgroundColor = .orange
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM