Search code examples
iosswiftxcodeswiftuihstack

How spacing is calculated in HStack


In SwiftUI when a horizontal array of Circles are made like this:

HStack(spacing : 4) {
  Foreach(0..<5) { index in
    Circle()
  }
}

How the (horizontal) spacing value of 4 is applied? Is it between the centre of two circles or from their edges instead?


Solution

  • Building on top of @Asperi's comment: the spacing is applied in the same way it's applied to Text or Button standard views, which is between the frames of the views.

    If you click on an element in the preview (this doesn't work when on live preview mode), you can see the frame of an element outlined in blue. The spacing is applied between the edges of the frames of each view.