首页 > 解决方案 > 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 ?

标签: uiscrollviewswiftuiright-to-lefthstack

解决方案


我找到了一个 hack 来使用.flipsForRightToLeftLayoutDirection(true).rotation3DEffect

就像滚动视图将被翻转一样,您需要将 HStack 的每个项目翻转到.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)

推荐阅读