![](/img/trans.png)
[英]App interace layout direction returning left-to-right for right-to-left language
[英]How ScrollView Layout Direction to right-to-left in SwiftUI?
我们如何在 swiftUI 的 Horizontal ScrollView 中设置从右到左的布局方向?
像这样的东西:
ScrollView(showsHorizontalIndicator: false) {
HStack {
VStack {
Image("1")
Text("one")
}
VStack {
Image("2")
Text("two")
}
VStack {
Image("3")
Text("three")
}
}
}
所以当模拟器运行时,我想在右侧看到Image("1")
并向右滑动以访问Image("3")
您可以通过在修改器下方提供您的view
来制作您想要从右到左的任何view
:
.flipsForRightToLeftLayoutDirection(true)
.environment(\.layoutDirection, .rightToLeft)
在你的情况下,它会是这样的:
ScrollView(showsHorizontalIndicator: false) {
HStack {
VStack {
Image("1")
Text("one")
}
VStack {
Image("2")
Text("two")
}
VStack {
Image("3")
Text("three")
}
}
}
.flipsForRightToLeftLayoutDirection(true)
.environment(\.layoutDirection, .rightToLeft)
好运达达什 ;)
你也可以使用我的助手。
import SwiftUI
struct ScrollViewRTL<Content: View>: View {
@ViewBuilder var content: Content
@Environment(\.layoutDirection) private var layoutDirection
var type: RowType
init(type: RowType, @ViewBuilder content: () -> Content) {
self.type = type
self.content = content()
}
@ViewBuilder var body: some View {
ScrollView(type.scrollAxis, showsIndicators: false) {
content
.rotation3DEffect(Angle(degrees: layoutDirection == .rightToLeft ? -180 : 0), axis: (
x: CGFloat(0),
y: CGFloat(layoutDirection == .rightToLeft ? -10 : 0),
z: CGFloat(0)))
}
.rotation3DEffect(Angle(degrees: layoutDirection == .rightToLeft ? 180 : 0), axis: (
x: CGFloat(0),
y: CGFloat(layoutDirection == .rightToLeft ? 10 : 0),
z: CGFloat(0)))
}
}
public enum RowType {
case hList
case vList
var scrollAxis: Axis.Set {
switch self {
case .hList:
return .horizontal
case .vList:
return .vertical
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.