-4

This code is something similar to my current UI. I have alot of view in between of Spacer(). I notice that when i add alot of Spacer() i get this error //Trailing closure passed to parameter of type 'HorizontalAlignment' that does not accept a closure// why is that?

import SwiftUI

 struct Phrases2: View {
   var body: some View {
    ZStack() {
        Text("")
        Text("")
        VStack() {
            Spacer()
            Text("")
            Spacer()
            Text("")
            Spacer()
            Text("")
            Spacer()
            Text("")
            Spacer()
            Text("")
            Spacer()
            Text("")
            Spacer()
            Text("")
            Spacer()
            Text("")
            Spacer()
            Text("")
            Spacer()
            Text("")
            Spacer()
        }
    }
  }
  }
zayden
  • 15
  • 4

2 Answers2

4

The issue here is the number of views in the stack. ViewBuilder in SwiftUI allows you to have up to 10 views in the HStack/VStack/List.

You can avoid the issue using Groups:

VStack {
    Group {
        Text("")
        Spacer()
        Text("")
        Spacer()
        Text("")
        Spacer()
    }
    Group {
        Text("")
        Spacer()
        Text("")
        Spacer()
        Text("")
        Spacer()
    }
}
stoikokolev
  • 484
  • 5
  • 9
  • Thank you. what about ForEach()? why it allow me to create so many text without group? – zayden Jul 19 '22 at 15:20
  • Using ForEach() is also good approach, here is a great article about it: https://www.hackingwithswift.com/quick-start/swiftui/how-to-create-views-in-a-loop-using-foreach – stoikokolev Jul 19 '22 at 15:25
2

You can use Group to wrap 10 Views. Then use it again as follows:

Group {
    some 10 views
}
Group {
    some other 10 views
}
Timmy
  • 4,098
  • 2
  • 14
  • 34