首页 > 解决方案 > SwiftUI:在 iPad 的分屏中多次推送视图

问题描述

在我使用 SwiftUI 的应用程序中,当我想在我的应用程序中实现拆分视图时,一切都在 iPhone 甚至 iPad 上运行良好,除非我在使用多个应用程序时进入分屏模式。这是 ContentView() 的代码,如果您需要更多,请随时询问。

import SwiftUI
import GoogleMobileAds

struct ContentView: View {

    @State var isAboutViewPresented = false

    var body: some View {
        NavigationView {
            DefaultView()
            NewView()
        }//.navigationViewStyle(StackNavigationViewStyle())
        // To get "full screen" on iPad -> not in a split view style
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .environment(\.locale, .init(identifier: "fr"))
    }
}


struct DefaultView: View {

    @State var isAboutViewPresented = false

    var body: some View {
        VStack {
            List {
                Section(header: Text("main"), footer: Text("showChartOfDesiredParameter")) {
                    NavigationLink(destination: ApertureStopChartView()) {
                        Text("apertureStopChart")
                    }
                    NavigationLink(destination: ShutterSpeedStopChartView()) {
                        Text("shutterSpeedStopChart")
                    }
                    NavigationLink(destination: ISOStopChartView()) {
                        Text("isoStopChart")
                    }
                }

                Section(header: Text("moreInfo")) {
                    NavigationLink(destination: WhatIsStopView()) {
                        Text("whatIsAStopInPhotography")
                    }
                }
            }.listStyle(GroupedListStyle())

            VStack {
                AdView().frame(width: 320, height: 50)
            }.edgesIgnoringSafeArea([.top, .leading, .trailing])

        }



            .navigationBarTitle(Text("Stop Chart"))
            .navigationBarItems(trailing:
                Button(action: {
                    self.isAboutViewPresented = true
                }) {Image(systemName: "info.circle")
                    .font(.title)
                    .foregroundColor(.blue)
                }.sheet(isPresented: $isAboutViewPresented, content: { AboutView(onDismiss: {
                    self.isAboutViewPresented = false
                })

                })
        )
    }
}


struct NewView: View {
    var body: some View {
        VStack {
            Image("StopChart-icon@1024px")
            .resizable()
                .frame(width: 400, height: 400, alignment: .center)
                .cornerRadius(23)
            HStack {
                Text("swipeToTheRight").font(.largeTitle)
                Image(systemName: "arrow.right").font(.largeTitle)
            }
        }
    }
}


struct AdView: UIViewRepresentable {

    func makeUIView(context: UIViewRepresentableContext<AdView>) -> GADBannerView {

        let banner = GADBannerView(adSize: kGADAdSizeBanner)

        banner.adUnitID = "ca-app-pub-3940256099942544/2934735716"
        banner.rootViewController = UIApplication.shared.windows.first?.rootViewController
        banner.load(GADRequest())
        return banner
    }

    func updateUIView(_ uiView: GADBannerView, context: UIViewRepresentableContext<AdView>) {

    }
}

GIF 向您展示错误

非常感谢您的帮助。

标签: iosswiftswiftuisplitviewsplit-screen

解决方案


我得到了你的代码示例来重现这个错误,它似乎是导致它的“listStyle(GroupedListStyle())”。删除它,错误就消失了。

import SwiftUI

struct ContentView: View {

    var body: some View {
        NavigationView {
            DefaultView()
        }
    }
}

struct DefaultView: View {

    @State var isAboutViewPresented = false

    var body: some View {
        VStack {
            List {
                NavigationLink(destination: Text("TEST1")) {
                    Text("TEST1")
                }
            }
            .listStyle(GroupedListStyle()) // <-- causing bug
        }
    }
}

推荐阅读