导航视图在 iPad 上的 SwiftUI 中无法正常工作

spo*_*oax 10 xcode ios swift swiftui

我正在尝试创建一个适用于 iPhone 和 iPad 的导航视图。目前我可以在 iPhone 上使用它,但是当在 iPad 上运行它时,导航视图无法正确显示我的主视图。见下文:

在此输入图像描述

  1. 这是我加载应用程序的时候
  2. 如果我按产品(左上),它会打开产品选项卡。
  3. 当我点击产品时,它会转到此屏幕
  4. 如果我单击“产品 1”(见第三张图片),它会将所有详细信息打开到另一个导航栏中。

我想要实现的是图像 4 不在导航选项卡中,而是全屏。我尝试从代码中删除 NavigationView,这似乎解决了问题并使其全屏显示。但是,我随后失去了导航视图按钮以允许用户查看其他产品。

这是我的代码的缩短版本(没有所有文本/图像详细信息):

var body: some View {
    NavigationView {
        ScrollView(.vertical, showsIndicators: false) {
            VStack(alignment: .center, spacing: 20) {
                ProductHeaderView(product: product)
                
                VStack(alignment: .leading, spacing: 15) {
                    Text(product.title)
                        .font(.largeTitle)
                        .fontWeight(.heavy)
                        .foregroundColor(product.gradientColors[1])
                    Text(product.headline)
                        .font(.headline)
                        .multilineTextAlignment(.leading)
                }
                .padding(.horizontal, 20)
                .frame(maxWidth: 640, alignment: .center)
            }
            .navigationBarTitle(product.title, displayMode: .inline)
            .navigationBarHidden(true)
        }
        .edgesIgnoringSafeArea(.top)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

预先感谢您的帮助 :)

编辑:

这是 ProductHeaderView.swift 代码:

var body: some View {
    ZStack {
        LinearGradient(gradient: Gradient(colors: product.gradientColors), startPoint: .topLeading, endPoint: .bottomTrailing)
        TabView{
            ForEach(0..<product.images.count, id: \.self) { item in
            Image(product.images[item])
                .resizable()
                .scaledToFit()
                .shadow(color: Color(red: 0, green: 0, blue: 0, opacity: 0.15), radius: 8, x: 6, y: 8)
                .scaleEffect(isAnimatingImage ? 1.0 : 0.6)
            }//: FOR LOOP
        }//: TAB VIEW
        .tabViewStyle(PageTabViewStyle())
        .padding(.vertical, 0)
    } //: ZSTACK
    .frame(height: 414)
    .onAppear(){
        withAnimation(.easeOut(duration: 0.5)){
            isAnimatingImage = true
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

示例项目: https: //github.com/spoax94/productsMinimal.git

小智 13

只需将此行添加为您的 NavigationView 中的修饰符:

.navigationViewStyle(StackNavigationViewStyle())
Run Code Online (Sandbox Code Playgroud)


Asp*_*eri 3

正如我所评论的,应该只有一个NavigationView,所以这里修复ProductDetailView并删除了多余的NavigationView

使用 Xcode 12 进行测试

struct ProductDetailView: View {
    
    var product: Product
    var products: [Product] = productData
    
    @State var showingPreview = false
    
    var body: some View {
            ScrollView(.vertical, showsIndicators: false) {
                 VStack(alignment: .center, spacing: 20) {

                      ProductHeaderView(product: product)

                      VStack(alignment: .leading, spacing: 15) {

                            Text(product.title)
                                 .font(.largeTitle)
                                 .fontWeight(.heavy)

                            Text(product.headline)
                                 .font(.headline)
                                 .multilineTextAlignment(.leading)

                            Text("Learn More About \(product.title)".uppercased())
                                 .fontWeight(.bold)
                                 .padding(0)

                            Text(product.description)
                                 .multilineTextAlignment(.leading)
                                 .padding(.bottom, 10)

                      }
                      .padding(.horizontal, 20)
                      .frame(maxWidth: 640, alignment: .center)
                 }
                 .navigationBarTitle(product.title, displayMode: .inline)
                 .navigationBarHidden(true)
            }
            .edgesIgnoringSafeArea(.top)
    }
}
Run Code Online (Sandbox Code Playgroud)