I'm working on trying to recreate the first SwiftUI Tutorial in code without using SwiftUI. In the example, it mentions using a Spacer
"to direct the layout to use the full width of the device":
VStack {
Text("Turtle Rock")
HStack {
Text("Joshua Tree National Park")
Spacer()
Text("California")
}
}
For the VStack
, HStack
, and Text
views, I can easily use UIStackView
and UILabel
. But for the Spacer
, I can't seem to find any equivalent in the standard library (no UISpacer
or anything like that), which makes me think this is something custom to SwiftUI. In the tutorial, it describes how this Spacer
works:
A spacer expands to make its containing view use all of the space of its parent view, instead of having its size defined only by its contents.
So how can I recreate the functionality of this Spacer
view programmatically? Do I just add a constraint to the UIStackView to make it full width? Or is there a way to add a subview to the UIStackView that makes it behave like it does in SwiftUI?