1

I am trying to add a navigation bar to my SwiftUI app using a TabView like this:

import SwiftUI

struct ContentView: View {
    @State var isLoggedIn: Bool = false
    var body: some View {
        NavigationView{
            if isLoggedIn {
                LogInPage()
            } else {
                TabView{
                    HomeScreen()
                        .tabItem {
                            Image(systemName: "house")
                            Text("Hem")
                        }
                    BookingsScreen()
                        .tabItem {
                            Image(systemName: "book")
                            Text("Bokningar")
                        }
                    ProfileScreen()
                        .tabItem {
                            Image(systemName: "person")
                            Text("Profil")
                        }
                }
            }
           
        }
    }
}

But I get two duplicates of the 'house' icon and the text "Home" under it, and I also get another icon with three dots and the text "More" under it, literally from nowhere as I haven't even mentioned anything else then a house, a book and a person. And when I press on it (the three dots) it shows a list of "person.fill" with the text "Profile" next to it. Here is an some image of how my TabView looks like and when I press on the three dots:

unexpected behaviour

2
  • Does it help to create a Label inside the .tabItem closure? Commented Mar 13, 2024 at 19:04
  • You mean replacing my Text() and Image() with a Label? Then no, it does not help Commented Mar 13, 2024 at 19:26

1 Answer 1

0

Wrap the views that duplicate in a VStack

       TabView{
            VStack{
                HomeScreen()
            }
                    .tabItem {
                        Image(systemName: "house")
                        Text("Hem")
                    }
                BookingsScreen()
                    .tabItem {
                        Image(systemName: "book")
                        Text("Bokningar")
                    }
           VStack {
                ProfileScreen()
           }
                    .tabItem {
                        Image(systemName: "person")
                        Text("Profil")
                    }
            }
Sign up to request clarification or add additional context in comments.

3 Comments

It will mess up my layout even more. By wrapping it inside a Stack I can no longer see any navigation bar at all. @lorem ipsum
@AranAli the TabView should always be on top. It is an error if it is inside a NavigationStack/View. Each tab needs its own, they must be mutually exclusive. This is per HIG.
Excuse me, yes you are right. Wrapping it inside a VStack do actually solve my problem. Silly me who misunderstood you at first...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.