SwiftUI: Tabview Repeating itself
Asked Answered
V

1

4

I'm trying to create a tabview for a macOS 10.15 app.

 TabView {      
        BookmarksView()
            .tabItem {
            Text("Bookmark Settings")
        }
    
    DisplaySettings()
        .tabItem {
            Text("Display Settings")
        }
}
     

And in any of my views included in the tab that has one element in the body it renders properly in the tab view.

struct BookmarksView: View {
    var body: some View {
        Text("Bookmarks View")
           .font(.title)
           .font(Font.body.bold())
             
    }
}

enter image description here

But if i add any other element in the view, the tab repeats and shows the added element in its own tab.

struct BookmarksView: View {
        var body: some View {
            Text("Bookmarks View")
               .font(.title)
               .font(Font.body.bold())

             Text("Testing")
               .font(.system(size: 15))
                  
        }
    }

enter image description here

Vulcanize answered 25/11, 2021 at 4:11 Comment(2)
I think you may need to add a tag to them or something.Hoelscher
Your code works well for me (no repeating) on macos 12.1 Beta, using Xcode 13.2-beta. Your issue may be due to macos 10.15. Note; it works well also when I target macos 10.15, but run it on macos 12.1 beta.Repulse
L
7

Try to wrap them in container (stack or something) explicitly, like

struct BookmarksView: View {
    var body: some View {
        VStack {                   // << this !!
          Text("Bookmarks View")
            .font(.title)
            .font(Font.body.bold())

          Text("Testing")
            .font(.system(size: 15))
        }               
    }
}
Leake answered 25/11, 2021 at 5:10 Comment(1)
I had this same issue once, I couldn't remember my solution but the moment I scrolled down and seen VStack { I knew what it was lol.Hoelscher

© 2022 - 2024 — McMap. All rights reserved.