Search code examples
iosswiftswiftuixcode11

How ScrollView Layout Direction to right-to-left in SwiftUI?


How can we set right to left layout direction in Horizontal ScrollView in swiftUI?

something like this:

ScrollView(showsHorizontalIndicator: false) {
    HStack {
        VStack {
            Image("1")
            Text("one")
        }
        VStack {
            Image("2")
            Text("two")
        }
        VStack {
            Image("3")
            Text("three")
        }
    }
}

so when simulator run, I want to see Image("1") in right hand side and swipe to right to can access to Image("3")


Solution

  • You can make any view you want right-to-left by giving your view below modifiers:

    .flipsForRightToLeftLayoutDirection(true)
    .environment(\.layoutDirection, .rightToLeft)
    

    In your case, it is going to be like this:

    ScrollView(showsHorizontalIndicator: false) {
        HStack {
            VStack {
                Image("1")
                Text("one")
            }
            VStack {
                Image("2")
                Text("two")
            }
            VStack {
                Image("3")
                Text("three")
            }
        }
    }
    .flipsForRightToLeftLayoutDirection(true)
    .environment(\.layoutDirection, .rightToLeft)
    

    GoodLuck dadash ;)