Are there maximum limits to VStack?

前端 未结 2 1629
醉话见心
醉话见心 2021-01-11 09:45

I started with a clean project and added 5 buttons and 5 spacers in a VStack and all is good. When I add the 6th spacer at the bottom, the code suddenly won\'t compile with

2条回答
  •  青春惊慌失措
    2021-01-11 10:12

    You can have at most 10 children in your VStack (and ZStack, HStack, and so forth). This is strictly related to their implementation and to the implementation of the @ViewBuilder closures in general. Look at the interfaces here below (you can find them through xCode, I slightly simplified them in order to be more readable):

    public struct ViewBuilder {    
        /// Builds an empty view from an block containing no statements, `{ }`.
        public static func buildBlock() -> EmptyView
    
        /// Passes a single view written as a child view (e..g, `{ Text("Hello") }`) through
        /// unmodified.
        public static func buildBlock(_ content: Content) -> Content where Content : View
    }
    
    extension ViewBuilder {    
        public static func buildBlock(_ c0: C0, _ c1: C1) -> TupleView<(C0, C1)> where C0 : View, C1 : View
    }
    
    extension ViewBuilder {
        public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2) -> TupleView<(C0, C1, C2)> where C0 : View, C1 : View, C2 : View
    }
    
    extension ViewBuilder {
        public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3) -> TupleView<(C0, C1, C2, C3)> where C0 : View, C1 : View, C2 : View, C3 : View
    }
    
    extension ViewBuilder {
        public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4) -> TupleView<(C0, C1, C2, C3, C4)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View
    }
    
    extension ViewBuilder {
        public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5) -> TupleView<(C0, C1, C2, C3, C4, C5)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View, C5 : View
    }
    
    extension ViewBuilder {
        public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6) -> TupleView<(C0, C1, C2, C3, C4, C5, C6)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View, C5 : View, C6 : View
    }
    
    extension ViewBuilder {
        public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7) -> TupleView<(C0, C1, C2, C3, C4, C5, C6, C7)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View, C5 : View, C6 : View, C7 : View
    }
    
    extension ViewBuilder {
        public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8) -> TupleView<(C0, C1, C2, C3, C4, C5, C6, C7, C8)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View, C5 : View, C6 : View, C7 : View, C8 : View
    }
    
    extension ViewBuilder {
        public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8, _ c9: C9) -> TupleView<(C0, C1, C2, C3, C4, C5, C6, C7, C8, C9)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View, C5 : View, C6 : View, C7 : View, C8 : View, C9 : View
    }
    

    As you can see you can build those kind of views with at most 10 children. Each buildBlock method takes an exact number of views as parameters. This is because @ViewBuilder closures, at least at the moment, don't support variadic arguments.

    A workaround can be:

    struct ContentView: View {
        var body: some View {
            VStack {
                Group {
                    //10 views here
                }
    
                Group {
                    //10 views here
                }
            }
        }
    }
    

提交回复
热议问题