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

Zor*_*gan 6 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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此ZStack我有一个TabView带有儿童的视图,所有视图都是橙色的ZStacks。但是,这些子视图(包括Settings()下面MapKitView()所示的)没有橙色状态栏。

设置()

struct Settings: View {

    var body: some View {
        ZStack {
            Color.orange
            VStack {
                NavigationView {
                       ...
                    }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

MapKitView()

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

    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如何使我所有视图中的状态栏变为橙色?

Pom*_*ule 20

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

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.
    }
Run Code Online (Sandbox Code Playgroud)

iOS 模拟器的屏幕截图,其中显示带有地图的应用程序,并且设备的状态栏具有模糊背景。


Joa*_*nes 10

ZStack {
...
}
.edgesIgnoringSafeArea(.vertical) // or .top
Run Code Online (Sandbox Code Playgroud)

.edgesIgnoreSafeArea在 iOS 14 中已弃用并替换为.ignoresSafeArea

ZStack {
...
}
.ignoresSafeArea(.all, edges: [.bottom, .top])
Run Code Online (Sandbox Code Playgroud)