HStack required to get columns in a Grid

I've looked at lots of Grid examples. I've seen it documented that views within a GridRow are treated as if they were in an HStack. That's not happening for me. I have to explicitly put them into an HStack or they are treated as new rows.

Here's my code:

   @State private var gridRows : Int = 4
   @State private var gridCols : Int = 2
   @State private var myItems : [Int] = Array(0...8)

	var body: some View
	{
		Group
		{
			if myItems.count > 0
			{
				ScrollView([.vertical])
				{
					Grid(alignment: .leading)
					{
						ForEach(0..<gridRows, id: \.self)
						{ rowNdx in
							GridRow
							{
								HStack  // <<---
								{
									ForEach(0..<gridCols, id: \.self)
									{ colNdx in
										Text("\(rowNdx) \(colNdx)")
									}
								}
							}
						}
					}
				}
			}
		}
	}

With the HStack I get (as expected).

0 0  0 1
1 0  1 1
2 0  2 1
3 0  3 1 

Without the HStack I get

0 0
0 1
1 0 
1 1
...

What have I done wrong?

Answered by ssmith_c in 813947022

I don't know. I tried your example, and with and without the HStack (and its accompanying braces, of course), I get the same result, the one you expect.

Xcode 16.1, macOS 14.7.1, built for macOS and iOS simulator.

Accepted Answer

I don't know. I tried your example, and with and without the HStack (and its accompanying braces, of course), I get the same result, the one you expect.

Xcode 16.1, macOS 14.7.1, built for macOS and iOS simulator.

I commented out the HStack in my code this morning and it started working. I have no idea. Thanks for your reply.

HStack required to get columns in a Grid
 
 
Q