23

I'm finishing an app that needs user data entry fields. I modeled it with a small number of data elements to streamline the development. Today I attempted to add additional elements and was astounded to find that I could only add 10 views to a view. So I tried the simplest of designs (below). If I added 11 "things" to the view it immediately presented the error on the top item, whatever it was:

"Argument passed to call that takes no arguments"

Doesn't matter whether I call the outside container a ScrollView, VStack, List or Form. Same behavior. Doesn't matter whether the Text/TextField sub units are in a VStack or not.

So I went back to basics - just added ten Text views. No problem. Add an eleventh and it blows up. Here's one of the variants - but all I need to do was add 10 simple Text views to get it to break.

I must be missing something really basic here. I checked for a newer release of Xcodebut I have Version 11.2 beta 2 (11B44), the latest.

@State private var textField1: String = "Pass to the ListCell"
@State private var textField2: String = "2"
//more of these


var body: some View {

    NavigationView {
        VStack {

            //extract the VStack and create a separate struct
            ListCell(tfString: textField1)

            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

            Text("6")
            Text("7")
            Text("8")
            Text("9")
            Text("10")
            //Spacer()
            //Text("11")
        }
    }
}

Any guidance would be appreciated.

JohnSF
  • 3,736
  • 3
  • 36
  • 72

3 Answers3

32

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")
        }
    }
}
Sorin Lica
  • 6,894
  • 10
  • 35
  • 68
6

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.

LuLuGaGa
  • 13,089
  • 6
  • 49
  • 57
4

Use Group as people suggest.

However, you can extend ViewBuilder to make builders for more than 10 views. I created an article that walk you through it at More than 10 views in SwiftUI extending ViewBuilder.

enter image description here

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172