【问题标题】:Navigation View not working properly in SwiftUI on iPad导航视图在 iPad 上的 SwiftUI 中无法正常工作
【发布时间】:2021-01-04 14:16:41
【问题描述】:

我正在尝试创建一个适用于 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)
    }
  }
}

提前感谢您的帮助:)

编辑:

这是 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
        }
    }
}

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

【问题讨论】:

  • 看起来您在ProductHeaderView 中还有另一个NavigationView,但您只能在单一视图层次结构中使用一个。
  • 嗨,我已经用我的 ProductHeaderView 更新了我的帖子,我没有在里面使用 NavigationView。
  • 你会准备最小可重现的例子吗?

标签: ios swift xcode swiftui


【解决方案1】:

正如我评论的那样,应该只有一个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)
    }
}

【讨论】:

  • 谢谢 Asperi,这并没有解决我遇到的问题,我还必须删除代码底部的 2 个 navigationBar 行。一旦我删除了这些以及导航视图,它就修复了所有内容。感谢您的帮助。
【解决方案2】:

我发现了问题所在。我删除了 navigationView 以及 2 行

.navigationBarTitle(product.title, displayMode: .inline)
.navigationBarHidden(true)

因为这隐藏了我视图顶部的导航按钮。

【讨论】:

    【解决方案3】:

    只需将此行作为修饰符添加到 NavigationView 中即可:

    .navigationViewStyle(StackNavigationViewStyle())
    

    【讨论】:

    • 应用这段代码后一切正常。谢谢,托马斯。
    猜你喜欢
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    相关资源
    最近更新 更多