Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't Add More Than 10 Items to View SwiftUI [duplicate]

Tags:

xcode

swiftui

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.

like image 324
JohnSF Avatar asked Oct 20 '19 05:10

JohnSF


People also ask

How to change text and font of text in contentview in Swift?

In the preview pane, Command-Click on the TextView and choose any attribute or inspect it. Let’s change text and font of text in ContentView.Swift file. Note that, body property only returns a single view. You can combine and embed multiple views in stacks, which group views together horizontally, vertically, or back-to-front.

How to use SwiftUI in Xcode?

Let’s play with SwiftUI. Open Xcode, and select Create a new Xcode project. Under iOS, select Single View App and click Next button. In the next window, give Product Name as SwiftUITutorial. Make sure SwiftUI is selected in User Interface dropdown. Click Next. After Next, select the project location and click on Create button.

How to create a circle image in SwiftUI view?

Choose File > New > File to open the template selector again. In the User Interface section, click to select SwiftUI View and click Next. Name the file CircleImage.swift and click Create.


3 Answers

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")
        }
    }
}
like image 71
Sorin Lica Avatar answered Oct 17 '22 21:10

Sorin Lica


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.

like image 40
LuLuGaGa Avatar answered Oct 17 '22 20:10

LuLuGaGa


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

like image 3
Geri Borbás Avatar answered Oct 17 '22 19:10

Geri Borbás