Form

#form #group #section

Overview

In SwiftUI a Form has a limitation of 10 child Items. When you add more than 10 items it will throw the error below. And this is not only for Form, this is true for all over SwiftUI.

"Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure"

struct ContentView: View {
    var body: some View {
        Form {
            Text("Hello, world!")
            Text("Hello, world!")
            Text("Hello, world!")
            Text("Hello, world!")
            Text("Hello, world!")
            Text("Hello, world!")
            Text("Hello, world!")
            Text("Hello, world!")
            Text("Hello, world!")
            Text("Hello, world!")
            Text("Hello, world!")
        }
    }
}

To workaround this limitation you can use Group

struct ContentView: View {
    var body: some View {
        Form {
            Group {
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
            }
            Group {
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
            }
        }
    }
}

...

Using Group to overcome that limitation does not make any difference in view. Both look the same as below.

...

But to split the Form you can use Section

struct ContentView: View {
    var body: some View {
        Form {
            Section {
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
            }
            Section {
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
                Text("Hello, world!")
            }
        }
    }
}

...

Form vs VStack

Sources

Videos

Articles / Documents

Last updated