SwiftUI ScrollView 纵横滚动
SwiftUI ScrollView vertical and horizontal scroll
我有一个滚动视图,我想根据 @State 更改滚动的方向。
struct HomeScreen: View {
@State var isVertical: Boolean = true
var body: some View {
VStack{
Button(action: {
self.isVertical.toggle()
}) {
Text("press me")
}.padding()
ScrollView(self.isVertical == true ? .horizontal : .vertical, showsIndicators: true){
if(self.isVertical){
HStack {
Text("a")
Text("b")
Text("c")
Text("d")
Spacer()
}
} else {
VStack{
Text("a")
Text("b")
Text("c")
Text("d")
}
}
}.padding()
}
}
当第一次在此屏幕上滚动视图是垂直的时,只要我单击按钮并反转状态,滚动视图就会改变并变得疯狂,垂直和水平滚动。我究竟做错了什么?我希望基于您正在垂直或水平滚动的滚动条。非常感谢
这是固定的变体(还更正了 isVertical
的处理以符合含义)
测试 Xcode 11.4 / iOS 13.4
struct HomeScreen: View {
@State var isVertical: Bool = true
var body: some View {
VStack{
Button(action: {
self.isVertical.toggle()
}) {
Text("press me")
}.padding()
ScrollView(self.isVertical ? .vertical : .horizontal, showsIndicators: true){
if(self.isVertical){
VStack {
Text("a")
Text("b")
Text("c")
Text("d")
Spacer()
}
} else {
HStack{
Text("a")
Text("b")
Text("c")
Text("d")
}
}
}.id(isVertical) // << main part !!
.padding()
}
}
}
我有一个滚动视图,我想根据 @State 更改滚动的方向。
struct HomeScreen: View {
@State var isVertical: Boolean = true
var body: some View {
VStack{
Button(action: {
self.isVertical.toggle()
}) {
Text("press me")
}.padding()
ScrollView(self.isVertical == true ? .horizontal : .vertical, showsIndicators: true){
if(self.isVertical){
HStack {
Text("a")
Text("b")
Text("c")
Text("d")
Spacer()
}
} else {
VStack{
Text("a")
Text("b")
Text("c")
Text("d")
}
}
}.padding()
}
}
当第一次在此屏幕上滚动视图是垂直的时,只要我单击按钮并反转状态,滚动视图就会改变并变得疯狂,垂直和水平滚动。我究竟做错了什么?我希望基于您正在垂直或水平滚动的滚动条。非常感谢
这是固定的变体(还更正了 isVertical
的处理以符合含义)
测试 Xcode 11.4 / iOS 13.4
struct HomeScreen: View {
@State var isVertical: Bool = true
var body: some View {
VStack{
Button(action: {
self.isVertical.toggle()
}) {
Text("press me")
}.padding()
ScrollView(self.isVertical ? .vertical : .horizontal, showsIndicators: true){
if(self.isVertical){
VStack {
Text("a")
Text("b")
Text("c")
Text("d")
Spacer()
}
} else {
HStack{
Text("a")
Text("b")
Text("c")
Text("d")
}
}
}.id(isVertical) // << main part !!
.padding()
}
}
}