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

时间:2020-06-07 12:56:17

标签: ios swift swiftui

我有一个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)
        }
    }
}

,在这个ZStack中,我有一个TabView,其子视图都具有橙色的ZStacks。但是,这些子视图(包括下面显示的Settings()MapKitView())没有橙色的状态栏。

设置()

struct Settings: View {

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

MapKitView()

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

    }
}

enter image description here

如何使状态栏在所有视图中显示为橙色?

2 个答案:

答案 0 :(得分:1)

s001# docker network create example_net
s001# docker build --network example_net -t example_image example_image
                     ^^^^^^^^^^^^^^^^^^^

答案 1 :(得分:0)

假设UITabBarController包含UIHostingController个实例,则可以通过在UIHostingController上设置视图背景颜色来解决此问题。

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