swiftui create tabs from array code example
Example: ForEach tabs swiftui
import SwiftUI
struct ContentView: View {
let tabData = [
TabItem(title: Text("Tab 1"), image: Image(systemName: "1"), tag: 1),
TabItem(title: Text("Tab 2"), image: Image(systemName: "2"), tag: 2),
TabItem(title: Text("Tab 3"), image: Image(systemName: "3"), tag: 3),
TabItem(title: Text("Tab 4"), image: Image(systemName: "4"), tag: 4)
]
@State private var selectedTab = 0
var body: some View {
TabView(selection: $selectedTab) {
ForEach(tabData) { tabItem in
Text("Screen: \(tabItem.tag)")
.tabItem {
tabItem.title
tabItem.image
}.tag(tabItem.tag)
}
}
}
}
struct TabItem: Identifiable {
var id = UUID()
var title: Text
var image: Image
var tag: Int
}