Arrangement of items in HStack from left to right in SwiftUI

Viewed 833

For my current SwiftUI project, I need to roll my own time pickers, and I want to put three or four ordinary Pickers one after the other in order to let the user choose hours, minutes, seconds, and (locale requiring) AM/PM (and variations on this which are unimportant to my question). My first thought on this is to put the pickers in an HStack. E.g.,

HStack {
    Text("H")
    Text("M")
    Text("S")
}

(And, yes, the Text views are just placeholders.) One problem: HStack orders everything from leading to trailing. For right-to-left languages, this is a problem, since this would result in the pickers being in the wrong order (S M H rather than H M S). How can I force the order items in an HStack to always be from left to right?

Note: I did think of the flipsForRightToLeftLayoutDirection(_:) view modifier, but trying to use this flipped over text, which is definitely not what I want.

Thanks in advance for any help anyone can provide.

1 Answers

It is possible to fix layout direction by explicit environment value

HStack {
    Text("H")
    Text("M")
    Text("S")
}.environment(\.layoutDirection, .leftToRight)
Related