SwiftUI - 从子视图转换回主视图后,删除 NavigationBar 中的额外 space

SwiftUI - Remove extra space in NavigationBar after transitioning back to Home view from subview

我是 SwiftUI 的新手,运行 遇到了一些小挑战。每当我从主页视图转到子视图然后返回主页视图时,我都会看到在导航视图中创建了额外的 space(请参阅链接的 GIF)。我想知道是否有人有任何建议 - 在此先感谢!

这是主屏幕:

struct Home: View {

@State private var view2 = false

var body: some View {
    NavigationView {
        VStack {
            Text("Home View!")
                .padding()
            
            NavigationLink(destination: View2(), isActive: $view2) { }
            
            Button {
                self.view2 = true
            } label: {
                Text("Go to next view")
            }
            
        }
        .navigationTitle("Home")
        
    }
    
} }

这里是子新(View2):

struct View2: View {

@State private var home = false

var body: some View {
    VStack {
        Text("This is View 2")
            .padding()
        
        NavigationLink(destination: Home().navigationBarBackButtonHidden(true), isActive: $home) { }
        
        Button {
            self.home = true
        } label: {
            Text("Go to Home view")
        }
        
    }
    .navigationTitle("View 1")
} }

Link 转 GIF: Visual GIF of the issue

每次通过 NavigationLink 推送新的 Home 时,您都会向层次结构添加另一个 NavigationView,因为 Home 有一个 NavigationView里面。

为了避免这种情况,您可以将 NavigationView 分开,而不是 link 到 View:

struct Home: View {
    var body: some View {
        NavigationView {
            View1() //<-- Here
        }
    }
}

struct View1 : View {
    @State private var view2 = false
    
    var body: some View {
        VStack {
            Text("Home View!")
                .padding()
            
            NavigationLink(destination: View2(), isActive: $view2) { }
            
            Button {
                self.view2 = true
            } label: {
                Text("Go to next view")
            }
            
        }
        .navigationTitle("Home")
    }
}

struct View2: View {
    
    @State private var home = false
    
    var body: some View {
        VStack {
            Text("This is View 2")
                .padding()
            
            NavigationLink(destination: View1() //<-- Here
.navigationBarBackButtonHidden(true), isActive: $home) { }
            
            Button {
                self.home = true
            } label: {
                Text("Go to Home view")
            }
            
        }
        .navigationTitle("View 2")
    }
}

话虽如此,我对这里的策略有点怀疑。它 似乎 不像推 new View1,你可能只想 back[=30] =] 到现有的。在这种情况下,您的代码可能如下所示:

struct Home: View {
    var body: some View {
        NavigationView {
            View1()
        }
    }
}

struct View1 : View {
    @State private var view2 = false
    
    var body: some View {
        VStack {
            Text("Home View!")
                .padding()
            
            NavigationLink(destination: View2(), isActive: $view2) { }
            
            Button {
                self.view2 = true
            } label: {
                Text("Go to next view")
            }
            
        }
        .navigationTitle("Home")
    }
}

struct View2: View {
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        VStack {
            Text("This is View 2")
                .padding()
            Button {
                presentationMode.wrappedValue.dismiss()
            } label: {
                Text("Go to Home view")
            }
            
        }
        .navigationTitle("View 2")
    }
}