简体   繁体   English

SwiftUI 将视图从第一个屏幕更改为 tabview 屏幕

[英]SwiftUI change view from first screen to tabview screen

I want to change views once the user taps 'get started' but due to having navigation view in my first view, it is showing back button on my next screen which I don't want.一旦用户点击“开始”,我想更改视图,但由于在我的第一个视图中有导航视图,它在我的下一个屏幕上显示了我不想要的后退按钮。 Please see the images attached below.请参阅下面的图片。

Code for the first view is below: import SwiftUI第一个视图的代码如下: import SwiftUI

struct ContentView: View {结构内容视图:查看{

var body: some View {
    NavigationView {
        VStack {
            Spacer()
            Text("LifePath")
                .font(.system(size: 48, weight: .semibold))
                .padding(.bottom)
            Spacer()
            
            
            
            NavigationLink(destination: ViewChanger()) {
                Text("Get Started")
                    .font(.headline)
                    .navigationBarBackButtonHidden(true)
            }
        }
        .padding()
    }
}

} }

back button showing on screen 2屏幕 2 上显示的后退按钮

First view第一视角

Change the location of your navigationBackButtonHidden modifier so that it actually modifies the view that you're going to (and not the NavigationLink label):更改您的navigationBackButtonHidden修饰符的位置,以便它实际修改您要访问的视图(而不是NavigationLink标签):

struct ContentView: View {
    
    var body: some View {
        NavigationView {
            VStack {
                Spacer()
                Text("LifePath")
                    .font(.system(size: 48, weight: .semibold))
                    .padding(.bottom)
                Spacer()
                
                
                
                NavigationLink(destination: ViewChanger()
                                .navigationBarBackButtonHidden(true) // <-- Here
                ) {
                    Text("Get Started")
                        .font(.headline)
                        
                }
            }
            .padding()
        }
    }
}

If you want not only the back button to be gone, but the entire header bar, you can use the .navigationBarHidden(true) modifier.如果您不仅希望后退按钮消失,而且希望整个 header 栏消失,则可以使用.navigationBarHidden(true)修饰符。

Also, if you run this on iPad at all, you probably want .navigationViewStyle(StackNavigationViewStyle()) added to the outside of your NavigationView此外,如果您完全在 iPad 上运行它,您可能希望.navigationViewStyle(StackNavigationViewStyle())添加到NavigationView的外部

If you use a NavigationLink (in a NavigationView), the view will be pushed.如果您使用 NavigationLink(在 NavigationView 中),则会推送视图。 If you want to replace the view, you can do this with an if statement.如果要替换视图,可以使用if语句来完成。

For example, this could be implemented like this:例如,这可以这样实现:

struct ContentView: View {
    @State var showSecondView: Bool = false

    var body: some View {
        if !showSecondView {
            NavigationView {
                VStack {
                    Spacer()
                    Text("LifePath")
                        .font(.system(size: 48, weight: .semibold))
                        .padding(.bottom)
                    Spacer()
            
                    Button(action: { showSecondView = true }) {
                        Text("Get Started")
                            .font(.headline)
                    }
            }
            .padding()
        } else {
            TabView {
                // ...
            }
        }
    }
}

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

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