2

I know I can set the spacing between views in an HStack like this:

HStack(spacing: 10) { .. }

If I had lets say 8 views in the HStack, is there a way to set the spacing to 1 only between two consecutive views of the 8 views in the stack overriding the initial HStack spacing set to 10 in this case?

I'd like all views in the stack to be 10 apart, but, two of the views I want to be 1 apart from each other.

I know I could put another HStack containing the two views and setting the spacing the sub stack to 1. I'm wondering if there's another way that might be better.

zumzum
  • 17,984
  • 26
  • 111
  • 172

2 Answers2

2

Maybe use padding instead? But "I know I could put another HStack containing the two views and setting the spacing the sub stack to 1" is probably the best way.

struct ContentView: View {
    var body: some View {
        HStack(spacing: 0) {
            ForEach(0..<10) { index in
                Rectangle()
                    .fill(Color.green)
                    .frame(width: 40, height: 40)
                    .padding(.horizontal, index == 0 || index == 1 ? 1 : 10)
            }
        }
    }
}

First 2 have 1 spacing, rest have 10

aheze
  • 24,434
  • 8
  • 68
  • 125
0

Well, what are you expecting as "another way"?

You have a main HStack where you can put VStacks, HStacks and ZStacks in.

So its Like..

HStack(spacing: 10) {

    HStack{}
    HStack{}

    HStack(spacing:1){
        HStack{}
    }
}

Dont get me wrong but I dont understand what your question is. This looks quite comfortable for me :-) Greetings.

Iskandir
  • 937
  • 1
  • 9
  • 21
  • I am wondering if there is a way to tell views inside the HStack to override the spacing setting as set on the HStack spacing... So, treat a view spacing between another as an exception from the standard spacing set on the HStack without having to use another HStack... – zumzum Jul 27 '21 at 18:06
  • I guess that's the wrong way to think about this? Should probably delete this question then. Not sure. – zumzum Jul 27 '21 at 18:10