Kamal Muradov
Kamal Muradov

Reputation: 35

How to enter the amount of shapes in for each identifiable loop SwiftUI

I am beginner in SwiftUI, updating my app and remaking it on SWIFTUI. Here is the screenshot what I had to do... Screenshot

I've successfully done actually the same, but the problem is that I have a bunch of this white boxes with text and etc. the Idea was to make it using foreach loop, and I didn't realize that there Rectangles, and I will be needed to always change the number of them. How can I make it using foreach or maybe there is another way of doing this? Here is the screenshot what I've done on Swift UI and the code.

SwiftUI screenshot


    VStack(spacing: 32){
                        HStack(alignment: .center,spacing: 8){
                            Rectangle()
                                .frame(width: 24, height: .infinity)
                                .foregroundColor(Color(red: 0.098, green: 0.303, blue: 0.476))
                                .padding(.leading, 20)
                                .padding(.top, 28)
                                .padding(.bottom, 12)
                            
                            Rectangle()
                                .frame(width: 24, height: .infinity)
                                .foregroundColor(Color(red: 0.098, green: 0.303, blue: 0.476))
                                .padding(.trailing, 20)
                                .padding(.top, 28)
                                .padding(.bottom, 12)
                                
                            Spacer()
                            
                            Image("high_volume_blue")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(width: 32, height: 32, alignment: .center)
                                .padding(.top, 8)
                                .padding(.trailing, 8)
                        }
                        
                        Text(NSLocalizedString("SoundSignals_In_Description_1", comment: ""))
                            .font(.system(size: 20))
                            .fontWeight(.light)
                            .foregroundColor(Color.black)
                            .multilineTextAlignment(.center)
                            .padding(.horizontal, 16)
                            .padding(.bottom, 20)
                        
                    }
                    .frame(width: UIScreen.screenWidth - 25, height: .infinity, alignment: .center)
                    .background(Color.white)

This is only the white box code.

Thanks!

Upvotes: 0

Views: 231

Answers (1)

Gal
Gal

Reputation: 546

if you want to display a list of Text views you can do this:

ScrollView {
   VStack(alignment: .leading, spacing: 10) {
      ForEach(boxes, id: \.self) { box in  //Boxes is whatever Array of values you have to display 
         Text("Write your text here")
            .font(.title)
            .onTapGesture { } // Also a possibility to tap on each box and execute some functionality
       // You can add here more views: Rectangle() or Image() or whatever
      }
   }
}
.padding(25)
.background(.white)

Upvotes: 1

Related Questions