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

brvekthn  于 2023-05-27  发布在  Swift
关注(0)|答案(2)|浏览(149)

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

    }
}

如何在所有视图中将状态栏设置为橙子?

fnvucqvd

fnvucqvd1#

例如,如果您想在状态栏下绘制一个漂亮的模糊。

YourView()
    .overlay(alignment: .top) {
        Color.clear // Or any view or color
            .background(.regularMaterial) // I put clear here because I prefer to put a blur in this case. This modifier and the material it contains are optional.
            .ignoresSafeArea(edges: .top)
            .frame(height: 0) // This will constrain the overlay to only go above the top safe area and not under.
    }

tzxcd3kk

tzxcd3kk2#

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

相关问题