Can't Add More Than 10 Items to View SwiftUI

Use Group {...} https://developer.apple.com/documentation/swiftui/group

var body: some View {

    NavigationView {
        VStack {

            //extract the VStack and create a separate struct
            ListCell(tfString: textField1)
            Group {
                VStack {
                    Text("Text Field")
                    TextField("Placeholder", text: $textField2)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .padding()
                }

                VStack {
                    Text("Text Field")
                    TextField("Placeholder", text: $textField3)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .padding()
                }
            }

            //more of the above VStacks
            Group {
                Text("6")
                Text("7")
                Text("8")
                Text("9")
                Text("10")
            }
            //Spacer()
            //Text("11")
        }
    }
}

ViewBuilders in SwiftUI take between 0 and 10 elements into their initializer anything more than that and you have to start grouping them using Group, VStack, HStack, List, ForEach and so on.

The best approach is to start extracting a few elements that belong together into separate Views, for example:

struct FormCell: View {

    @Binding var inputString: String

    var body: some View {
        VStack {
            Text("Text Field")
            TextField("Placeholder", text: $inputString)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .padding()
        }
    }
}

Next if you have a few of them, you can group them using ForEach and List or VStack.

Tags:

Xcode

Swiftui