uiscrollviewswiftuiright-to-lefthstack

SwiftUI ScrollView horizontal RTL not working correctly


I'm trying to support RTL mode on my ios , which was built using swiftUI every is fine while doing this to change the layour direction :

.environment(\.layoutDirection, .rightToLeft)

Only with horizontal ScrollView , it's not work correctly

when i do this :

 ScrollView(.horizontal) {
            HStack{
                Text("b1")
                Text("b2")
                Text("b3")
            }
        } .environment(\.layoutDirection, .rightToLeft)

Items positions will rotate , but the HSTACK will stay always on the left like the screenshot below : enter image description here

Any solution or hack to make it to the right ?


Solution

  • I find a hack to do this using .flipsForRightToLeftLayoutDirection(true) and .rotation3DEffect

    Like that the scrollview will be flipped , than you need to flip each item of HStack to .rotation3DEffect

     ScrollView(.horizontal) {
                HStack{
                    Text("b1")
                        .rotation3DEffect(Angle(degrees: 180), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0)))
                    Text("b2")
                     .rotation3DEffect(Angle(degrees: 180), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0)))
                    Text("b3")
                     .rotation3DEffect(Angle(degrees: 180), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0)))
                }
            }.flipsForRightToLeftLayoutDirection(true)
            .environment(\.layoutDirection, .rightToLeft)