首页 > 解决方案 > SwiftUI TabView 问题

问题描述

我正在构建一个应用程序,而且我是 SwiftUI 的初学者,所以这就是我遇到的问题。我需要一个首先显示的登录页面,并且我已经为它创建了一个视图,您可以查看它。

     struct LandingView: View {
var body: some View {   
    NavigationView{
    VStack(alignment: .center){
        Image("exxxlogo")
        Spacer()
        Text("Welcome")
            .font(.system(size: 38))
            .fontWeight(.bold)
            .padding(.horizontal, 40)
            .multilineTextAlignment(.center)
            .foregroundColor(Color.white)
        Text("Click below to continue to the app")
            .font(.system(size: 16))
             .padding(.horizontal, 20)
            .multilineTextAlignment(.center)
            .foregroundColor(Color.white)
            .padding(.bottom, 35)
        NavigationLink(destination: HomeView()){
            Text("CONTINUE ")
                  .frame(width: 250, height: 50, alignment: .center)
                  .background(Color.red)
                  .foregroundColor(Color.white)
        }
        
    }
    
        .background(
            
            Image("backgroundlanding")
                
                .frame(maxWidth: .infinity,maxHeight: .infinity)
                .aspectRatio(contentMode: .fill)
                                .edgesIgnoringSafeArea(.top)
                .edgesIgnoringSafeArea(.bottom)
               
    )
}

而且我还为 tabview 创建了视图,以将它们重定向到 Home、Shop 等我已经完成了我的 Contentview

    TabView {
        
       
               HomeView()
                .tabItem{
                    Image(systemName: "house")
                    Text("Home")
                }
        
                GamesView()
                    .tabItem{
                        Image(systemName: "gamecontroller")
                        Text("Games")
        
                
                    }
        
        ShopView()
         .tabItem{
             Image(systemName: "bag")
             Text("Shop")
            }
        
        MoreView()
            .tabItem{
                Image(systemName: "gear")
                Text("More")
        
            }.accentColor(.red)

并在 app.swift 主文件中将窗口组更改为landingview,使其首先弹出。但问题是当您单击landingview 中的按钮时,navigationlink 会将您重定向到主视图,但下面没有标签视图,只是空白

标签: iosswiftswiftui

解决方案


您说您的第二个代码示例来自ContentView. 如果你想展示TabView你在其中拥有的东西ContentView,那么你NavigationLink应该指向ContentView,而不是HomeView

NavigationLink(destination: ContentView()) {
            Text("CONTINUE ")
                  .frame(width: 250, height: 50, alignment: .center)
                  .background(Color.red)
                  .foregroundColor(Color.white)
}

推荐阅读