首页 > 解决方案 > ZStack 背景颜色不显示

问题描述

这是我的ZStack

struct HomeView: View {
    @State var selectedTab = 1
    var body: some View {
        ZStack {
            TabView(selection: $selectedTab) {
                Settings()
                .tabItem {
                        Image(systemName: "gear").font(.system(size: 25))
                    Text("Settings")

                }.tag(0)
                ZStack {
                    Color.black
                    MapView()
                    VStack {
                        Spacer()
                        Button(action: {}){
                            Text("Offline")
                                .font(.system(size: 25))
                                .fontWeight(.bold)
                                .foregroundColor(Color.white)
                        }.padding().background(Color.black).opacity(0.7).cornerRadius(40.0)
                    }.padding(40)
                }.edgesIgnoringSafeArea(.top)
                .tabItem {
                    Image(systemName: "location.circle.fill").font(.system(size: 25))
                    Text("Home")
                }.tag(1)
                ProfileView()
                .tabItem {
                    Image(systemName: "person").font(.system(size: 25))
                    Text("Profile")
                }.tag(2)
            }.accentColor(Color.white)
            .font(.headline)
        }.edgesIgnoringSafeArea(.top)
    }

}

在上面你可以看到我申请Color.blackZStack封闭MapView()- 但 MKMapView没有黑色背景:

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

    }

    final class Coordinator: NSObject, MKMapViewDelegate {
        var control: MapView

        init(_ control: MapView) {
            self.control = control
        }
    }

    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
}

在此处输入图像描述

知道为什么吗?

标签: iosswiftswiftuimapkit

解决方案


推荐阅读