SwiftUI - ForEach Loop in code is not displaying on tab

Viewed 131

In my .tabItem 3 my loop is not displaying in the preview. Am I missing something? Still learning swift...

            .tabItem {
                VStack {
                    Image(systemName: "gamecontroller")
                    Text("Tab 3")
                    VStack {
                        List{
                            ForEach(0..<100) { index in
                                Text("This is tab 3!")
                            }
                        }
                    }
                }
            }
1 Answers

Your ForEach is inside the .tabItem, which is the tab bar icon. There's no way that tiny button can display 100 items.

I assume you want the ForEach to be that tab's screen?

Tab 3 active, showing 100 rows of 'This is tab 3!'

Just put the VStack before the .tabItem.

VStack {
    List{
        ForEach(0..<100) { index in
            Text("This is tab 3!")
        }
    }
}
.tabItem {
    VStack {
        Image(systemName: "gamecontroller")
        Text("Tab 3")
        
    }
}
Related