Hello
Is there a view to loop different views, for example in a ForEach loop
Here is what I mean:
Is there a way to put the 4 views in a loop instead of making four different Text Views(they are different views)
Thank your for your time
Is there a view to loop different views, for example in a ForEach loop
Here is what I mean:
Code Block import SwiftUI struct ContentView: View { @State var view1 = false @State var view2 = false @State var view3 = false @State var view4 = false private var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible())] var textSize: CGFloat { if UIDevice.current.userInterfaceIdiom == .pad { return 48 } return 23 } var title: CGFloat { if UIDevice.current.userInterfaceIdiom == .pad { return 60 } return 34 } var height: CGFloat { if UIDevice.current.userInterfaceIdiom == .pad { return 0.15 } return 0.15 } var weight: CGFloat { if UIDevice.current.userInterfaceIdiom == .pad { return 0.44 } return 0.43 } var body: some View { NavigationView{ GeometryReader { geometry in ScrollView(.vertical, showsIndicators: true) { LazyVGrid(columns: gridItemLayout, spacing: 18){ Group{ Text("View1") .foregroundColor(.black) .frame(width: geometry.size.width * weight, height: geometry.size.height * height) .background(Color.white) .onTapGesture { view1 = true } .sheet(isPresented: $view1) { View1() } Text("View2") .foregroundColor(.black) .frame(width: geometry.size.width * weight, height: geometry.size.height * height) .background(Color.white) .onTapGesture { view2 = true } .sheet(isPresented: $view2) { View2() } Text("View3") .foregroundColor(.black) .frame(width: geometry.size.width * weight, height: geometry.size.height * height) .background(Color.white) .onTapGesture { view3 = true } .sheet(isPresented: $view3) { View3() } Text("View4") .foregroundColor(.black) .frame(width: geometry.size.width * weight, height: geometry.size.height * height) .background(Color.white) .onTapGesture { view4 = true } .sheet(isPresented: $view4) { View4() } } }.padding() } } .navigationTitle("Title") } } }
Is there a way to put the 4 views in a loop instead of making four different Text Views(they are different views)
Thank your for your time
Would this do it ?
You can go one step further
Declare
and replace the switch lines 57 to 63 by:
Code Block import SwiftUI struct ContentView: View { @State var view1 = false @State var view2 = false @State var view3 = false @State var view4 = false @State var views = [false, false, false, false] private var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible())] private var viewTexts = ["View1", "View2", "View3", "View4"] var textSize: CGFloat { if UIDevice.current.userInterfaceIdiom == .pad { return 48 } return 23 } var title: CGFloat { if UIDevice.current.userInterfaceIdiom == .pad { return 60 } return 34 } var height: CGFloat { if UIDevice.current.userInterfaceIdiom == .pad { return 0.15 } return 0.15 } var weight: CGFloat { if UIDevice.current.userInterfaceIdiom == .pad { return 0.44 } return 0.43 } var body: some View { NavigationView{ GeometryReader { geometry in ScrollView(.vertical, showsIndicators: true) { LazyVGrid(columns: gridItemLayout, spacing: 18) { Group { ForEach(0..<views.count) { i in Text(viewTexts[i]) .foregroundColor(.black) .frame(width: geometry.size.width * weight, height: geometry.size.height * height) .background(Color.white) .onTapGesture { views[i] = true } .sheet(isPresented: $views[i]) { switch i { case 0: View1() case 1: View2() case 2: View3() case 3: View4() default: View1() } } } } }.padding() } } .navigationTitle("Title") } } } struct View1: View { var body: some View { Text("View1") } } struct View2: View { var body: some View { Text("View2") } } struct View3: View { var body: some View { Text("View3") } } struct View4: View { var body: some View { Text("View4") } }
You can go one step further
Declare
Code Block let Views : [AnyView] = [AnyView(View1()), AnyView(View2()), AnyView(View3()), AnyView(View4())]
and replace the switch lines 57 to 63 by:
Code Block .sheet(isPresented: $views[i]) { Views[i] }