Zorgan
Zorgan

Reputation: 9123

How to change the status bar background color in SwiftUI?

I have a ZStack which has Color.orange set on it:

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. However these child views, including Settings() and MapKitView() shown below, do not have an orange status bar.

Settings()

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

How can I make the status bar orange across all my views?

Upvotes: 7

Views: 9101

Answers (2)

Joannes
Joannes

Reputation: 2940

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

.edgesIgnoreSafeArea is deprecated in iOS 14 and replaced with .ignoresSafeArea

ZStack {
...
}
.ignoresSafeArea(.all, edges: [.bottom, .top])

Upvotes: 10

Cl&#233;ment Cardonnel
Cl&#233;ment Cardonnel

Reputation: 5227

If, for example, you wanted to draw a nice blur under the status bar.

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.
    }

A screenshot of the iOS simulator where an app with a map is shown and the device's status bar has a blur background.

Upvotes: 20

Related Questions