What I would like to achieve. regardless the width of text width at both side the button should always at the center of the HStack.

HStack {
Text("Foooooooo")
Spacer(minLength: 5)
Button(action: { }) {
Text("Bar")
}
Spacer()
Text("Baz")
}
.font(.system(size: 16, weight: .heavy, design: .rounded))
.padding()

I also tried to use GeometryReader and set frame size for each Text and Button in the view however there are two problems,
GeometryReader would occupies the entire view the parent offers to it instead of the actual intrinsic content size, the space only enough for Text, Spacer and Button
Text could not be left align so does the string inside the last Text couldn't be right alignedHere is possible approach for your case. Demo prepared & tested with Xcode 12 / iOS 14

HStack {
Spacer()
.overlay(Text("Foooooooo"), alignment: .leading)
Button(action: { }) {
Text("Bar")
}
Spacer()
.overlay(Text("Baz"), alignment: .trailing)
}
.font(.system(size: 16, weight: .heavy, design: .rounded))
.padding()
backup
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With