3

我是 SwiftUI 的新手,遇到一个错误,当我使用太多导航链接时,我的整个屏幕变灰。在研究错误时我找不到任何解决方案。我正在最新版本的 Xcode 12.4 上运行该项目。我目前的设置是有 2 个不同的 swiftUI 视图,每个视图都包含一个导航链接到另一个。

这就是它的样子

代码:

PageOne.swift

struct PageOne: View {
    var body: some View {
        NavigationView {
            VStack {
                Text("This is page 1")
                    .font(.system(size: 36, weight: .bold))
                    .padding(.bottom)
                
                NavigationLink(
                    destination: PageTwo(),
                    label: {
                        VStack {
                            Text("Go to Page 2")
                                .font(.system(size: 24, weight: .medium))
                                .foregroundColor(.white)
                                .frame(width: 200, height: 50, alignment: .center)
                                .background(Color.blue)
                                .cornerRadius(12)
                            
                        }
                    })
            }
        }
        .navigationBarHidden(true)
        .navigationBarBackButtonHidden(true)
    }
}

PageTwo.swift

struct PageTwo: View {
    var body: some View {
        NavigationView {
            VStack {
                Text("This is page 2")
                    .font(.system(size: 36, weight: .bold))
                    .padding(.bottom)
                
                NavigationLink(
                    destination: PageOne(),
                    label: {
                        VStack {
                            Text("Go to Page 1")
                                .font(.system(size: 24, weight: .medium))
                                .foregroundColor(.white)
                                .frame(width: 200, height: 50, alignment: .center)
                                .background(Color.blue)
                                .cornerRadius(12)
                            
                        }
                    })
            }
        }
        .navigationBarHidden(true)
        .navigationBarBackButtonHidden(true)
    }
}

项目文件

4

1 回答 1

2

您应该在视图层次结构中只有一个NavigationView

尝试NavigationView在根级别创建一个:

struct ContentView: View {
    var body: some View {
        NavigationView {
            PageOne()
                .navigationBarHidden(true)
                .navigationBarBackButtonHidden(true)
        }
    }
}

然后NavigationView从子视图中删除:

struct PageOne: View {
    var body: some View {
        VStack {
            Text("This is page 1")
                .font(.system(size: 36, weight: .bold))
                .padding(.bottom)
            
            NavigationLink(
                destination: PageTwo(),
                label: {
                    VStack {
                        Text("Go to Page 2")
                            .font(.system(size: 24, weight: .medium))
                            .foregroundColor(.white)
                            .frame(width: 200, height: 50, alignment: .center)
                            .background(Color.blue)
                            .cornerRadius(12)
                        
                    }
                })
        }
        .navigationBarHidden(true)
        .navigationBarBackButtonHidden(true)
    }
}
struct PageTwo: View {
    var body: some View {
        VStack {
            Text("This is page 2")
                .font(.system(size: 36, weight: .bold))
                .padding(.bottom)
            
            NavigationLink(
                destination: PageOne(),
                label: {
                    VStack {
                        Text("Go to Page 1")
                            .font(.system(size: 24, weight: .medium))
                            .foregroundColor(.white)
                            .frame(width: 200, height: 50, alignment: .center)
                            .background(Color.blue)
                            .cornerRadius(12)
                        
                    }
                })
        }
        .navigationBarHidden(true)
        .navigationBarBackButtonHidden(true)
    }
}
于 2021-02-23T11:55:07.467 回答