Home > Enterprise >  How to align two items in a box on different sites in SwiftUI
How to align two items in a box on different sites in SwiftUI

Time:08-29

I am currently working on a little Project and I really dont know how to fix some issue, the problem is that i have no idea how to align two items in a horizontal stack on different sites. Heres a screenshot of the current state:

Current State

and that's how it should look like:

How it should look like

this is the current code:

    var body: some View {
            Button {
            }
        label: {
            HStack{
                VStack(alignment: .leading) {
                        Text("Max Mustermann")
                            
                        Text("Mustermann GmbH")
                            .fontWeight(.thin)
                }
                Image(systemName: "arrow.forward.circle")
                }
            .frame(minWidth: 320, minHeight: 50, alignment: .leading)
                .foregroundColor(.black)
                .padding()
                .overlay(
                    RoundedRectangle(cornerRadius: 15)
                    .stroke(.gray, lineWidth: 1))
            }
       
    }
}

I hope somebody can help me :) Thank you!

CodePudding user response:

Seems like a Spacer is what you need :) Just add it in between the Views in the HStack like so:

HStack{
    VStack(alignment: .leading) {
        Text("Max Mustermann")

        Text("Mustermann GmbH")
            .fontWeight(.thin)
    }

    Spacer() // The New Spacer Here

    Image(systemName: "arrow.forward.circle")
}

The documentation linked above illustrates pretty well what can be achieved by using a Spacer :)

Good luck!

  • Related