简体   繁体   English

NavigationView 在 ScrollView 中没有正确显示

[英]NavigationView doesn't show up correctly in ScrollView

I have encountered this problem during UI building.我在构建 UI 时遇到过这个问题。 Previously, I've been asking here about NavigationView and why NavigationLink wasn't working.以前,我一直在这里询问 NavigationView 以及 NavigationLink 为何不起作用。 I fixed this problem with NavigationView at the top of my ScrollView.我用 ScrollView 顶部的 NavigationView 解决了这个问题。 Now FrameView works just fine: opens and closes it in preview, which means I'm doing it right.现在 FrameView 工作得很好:在预览中打开和关闭它,这意味着我做对了。

在此处输入图像描述

在此处输入图像描述

Here's the catch: When I add this LongFrameScrollView to my main view HomeView, it appears in some sort of frame and doesn't open link in full screen.这里有一个问题:当我将这个 LongFrameScrollView 添加到我的主视图 HomeView 时,它出现在某种框架中并且不会在全屏模式下打开链接。

在此处输入图像描述

What am I doing wrong?我究竟做错了什么? How to fix that?如何解决? Providing the code:提供代码:

// Long frame view

import SwiftUI

struct LongFrameView: View {
    
    var body: some View {
            NavigationLink {
                PlayerView()
            } label: {
                ZStack {
                    Rectangle()
                        .fill(LinearGradient(gradient: Gradient(colors: [Color(red: 0.268, green: 0.376, blue: 0.587), Color(red: 0.139, green: 0.267, blue: 0.517)]),
                                             startPoint: .leading,
                                             endPoint: .trailing))
                        .frame(width: 310, height: 62)
                        .cornerRadius(8)
                    
                    HStack {
                        Image("mountains")
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: 70, height: 62)
                            .cornerRadius(8, corners: [.topLeft, .bottomLeft])
                        
                        
                        VStack(alignment: .leading) {
                            
                            Text("Sense of anxiety")
                                .font(.custom("Manrope-Bold", size: 14))
                                .foregroundColor(.white)
                            Text("11 MIN")
                                .font(.custom("Manrope-Medium", size: 12))
                                .foregroundColor(.white)
                            
                        }
                        
                        Spacer()
                        
                    }
                }
                .frame(width: 310, height: 62)
            }
    }
}

// Scroll View with LongFrame. Works fine in the preview
struct LongFrameScrollView: View {
        
    let rows = Array(repeating: GridItem(.fixed(70), spacing: 10, alignment: .leading), count: 2)
    
    var body: some View {
        NavigationView {
            ScrollView(.horizontal, showsIndicators: false) {
                LazyHGrid(rows: rows, spacing: 10) {
                    
                    // PLACEHOLDER UNTIL SERVER IS READY
                    
                    LongFrameView()
                    
                    LongFrameView()
                    
                    LongFrameView()
                    
                    LongFrameView()
                    
                }
            }
            .padding([.horizontal, .bottom], 10)

        }
    }
}

    var body: some View {
        
ScrollView(.vertical, showsIndicators: false) {
                
                VStack(alignment: .center) {
                    ///
                    
              // Long frame. There is more view, but we'll just ignore them
                    LongFrameScrollView()
                        .padding(.bottom)

                    ///

                }
                .padding(.top, 10)
                
            }
            .background(
                Image("background2")
                    .resizable()
                    .aspectRatio(contentMode: .fill)
        )
        }
}

The reason why it wasn't working correctly is because it was in TabView.它不能正常工作的原因是因为它在 TabView 中。 So, in order to work, our TabView must be in NavigationView and we must delete from all child views NavigationView.所以,为了工作,我们的 TabView 必须在 NavigationView 中,我们必须从所有子视图中删除 NavigationView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM