swiftswiftuiios16swiftui-picker

SwiftUI Picker Options Flipped in RTL


As you can see in the attached image, the picker options are not aligned automatically to the right-hand side for RTL languages, and also flipped.

enter image description here

The picker is nothing fancy either, the standard way.

struct ContentView: View {
    
    @State private var selectedDay: String = "יום שבת"
    private let days: [String] = ["יום שבת", "יוֹם רִאשׁוֹן", "יוֹם שֵׁנִי"]
    
    var body: some View {
        Picker(selection: $selectedDay) {
            ForEach(days, id: \.self) { day in
                Text(day).tag(day)
            }
        } label: {
            Text("")
        }
    }
}

I have changed the development localization to Hebrew so RTL alignments are automatically done.

I have tried to force the alignment with

.environment(\.layoutDirection, .rightToLeft)

but nothing has worked.


Solution

  • I came across this question seems a year later since I asked it XD. I have managed to get it to work in RTL :

    1. Edit scheme > App Language > Arabic (or your RTL language or 'Right-to-Left Pseudolanguage')

    If you only do this step, the RTL layout will appear correctly in the simulator but not on a real device (unless the device's language is RTL)

    1. Set the locale language

    This will force the app's preferred language to always remain your RTL language.

    @main
    struct AppName: App {
    
        init() {
            UserDefaults.standard.set(["ar"], forKey: "AppleLanguages")
            }
    
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }