Home > Net >  Swift navigation bar not appearing
Swift navigation bar not appearing

Time:10-26

I’m new to Swift. Currently trying to build a test app.

My tab nav bar won't appear when i preview the app. When in preview, i can click it and switch between pages, but i can't see it. Created a storyboard with a tab bar controller and view controllers.

Code below. Can't upload images sadly due to being new.

`

import SwiftUI

@main
struct Login_PageApp: App {
    var body: some Scene {
        WindowGroup {
            TabView {
                WTF_Home()
                WTF_Guides()
                WTF_Guides2()
                WTF_Book()
                WTF_Help()
            }
        }
    }
}

I've tried looking up solutions but have had no success. Must guides on navigation fixes are from years ago. Ideally i need guides / fixes from 2022.

I've even tried using the 'Navigation Controller' in the storyboard because people suggested it, no success there either.

Any help would be much appreciated!

CodePudding user response:

Add the modifier .tabItem{Image(systemName: "whatever_image")} to each of the pages in your TabView.

CodePudding user response:

Here is complete professional example how you can add tabbar into you application. First Make a separate tabbar controller and then call it from WindowGroup{} which is the start point for you application.

Tabbarcontroller.swift

struct TabBarControllerView: View {
    
    @State private var tabSelection = 0
    
    init() {
        UITabBar.appearance().backgroundColor = .white
        UITabBar.appearance().unselectedItemTintColor = .gray
    }
    
    var body: some View {
        TabView(selection: $tabSelection) {
            
            HomeView()
                .tabItem {
                    VStack {
                        Image(ImageName.home.rawValue)
                            .renderingMode(.template)
                        Text("Home")
                    }
                    
                }
                .tag(0)
            
            DailyCheckListView()
            
                .tabItem {
                    
                    VStack {
                        Image(ImageName.checkList.rawValue)
                            .renderingMode(.template)
                        Text("CheckList")
                    }
                    
                }
                .tag(1)
            
            
            GratitudeJournalView()
                .tabItem {
                    VStack {
                        Image(ImageName.journal.rawValue)
                            .renderingMode(.template)
                        Text("Journal")
                    }
                    
                }
                .tag(2)
            
            TDLHostingView()
                .tabItem {
                    VStack {
                        Image(ImageName.TDL.rawValue)
                            .renderingMode(.template)
                        Text("ToDoList")
                    }
                    
                }
                .tag(3)
            
        }
        .accentColor(Color(ColorName.titleColor.rawValue))
        
    }
}
}

struct TabBarControllerView_Previews: PreviewProvider {
    static var previews: some View {
        TabBarControllerView()
    }
}

Now you need to call the TabBarControllerView() from your application @main file.

Note just need to add single navigationView at start of your application which will automatically work for each view in tabbar.

Login_PageAapp.swift

import SwiftUI
    @main
struct Login_PageApp: App {
    var body: some Scene {
        WindowGroup {
            NavigationView {
                TabBarControllerView()
            }
            
        }
    }
}
  • Related