SwiftUI - 从底部过渡时水平滚动视图锁定
SwiftUI - Horizontal ScrollView locks up when transitioning from the bottom
基本上,每当我尝试使用 AnyTransition.move(edge: .bottom)
转换水平滚动的 ScrollView 时,应用程序就会冻结,并且内存会不断增加。我已经成功重现了以下问题:
struct ContentView: View {
@State private var showScroll: Bool = false
var body: some View {
VStack {
Spacer()
Button(action: {
withAnimation {
self.showScroll = true
}
}, label: {
Text("Hit me")
}).padding()
.background(Capsule().fill())
Spacer()
if showScroll {
scrollView
}
}
}
var scrollView: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
Text("Horizontal list")
Text("Horizontal list")
Text("Horizontal list")
Text("Horizontal list")
}
}
.frame(height: 100)
.transition(.move(edge: .bottom))
}
}
将 ScrollView 轴更改为 .vertical
可防止应用程序挂起,将过渡更改为不同的边缘(例如 .leading
)也是如此。
有没有其他人遇到过这样的事情?
确认在模拟器中描述的行为,在预览中一切正常。 Xcode 11.2。像下面这样在 VStack 中包装解决了这个问题。
var scrollView: some View {
VStack {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
Text("Horizontal list")
Text("Horizontal list")
Text("Horizontal list")
Text("Horizontal list")
}
}
.frame(height: 100)
}
.transition(.move(edge: .bottom))
}
您需要配置HStack。
struct ContentView: View {
@State private var showScroll: Bool = false
var body: some View {
VStack {
Spacer()
Button(action: {
withAnimation {
self.showScroll = true
}
}, label: {
Text("Hit me")
}).padding()
.background(Capsule().fill())
Spacer()
if showScroll {
scrollView
}
}
}
var scrollView: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
Text("Horizontal list")
Text("Horizontal list")
Text("Horizontal list")
Text("Horizontal list")
}.frame(maxHeight: .infinity)
}
.frame(height: 100)
.transition(.move(edge: .bottom))
}
}
使用 .frame(minHeight: 0, maxHeight: .greatestFiniteMagnitude)
而不是 .frame(maxHeight: .infinity)
给了我更好的结果。 IE 。我可以为 .frame(height: 100)
设置任何值,它不会冻结。
基本上,每当我尝试使用 AnyTransition.move(edge: .bottom)
转换水平滚动的 ScrollView 时,应用程序就会冻结,并且内存会不断增加。我已经成功重现了以下问题:
struct ContentView: View {
@State private var showScroll: Bool = false
var body: some View {
VStack {
Spacer()
Button(action: {
withAnimation {
self.showScroll = true
}
}, label: {
Text("Hit me")
}).padding()
.background(Capsule().fill())
Spacer()
if showScroll {
scrollView
}
}
}
var scrollView: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
Text("Horizontal list")
Text("Horizontal list")
Text("Horizontal list")
Text("Horizontal list")
}
}
.frame(height: 100)
.transition(.move(edge: .bottom))
}
}
将 ScrollView 轴更改为 .vertical
可防止应用程序挂起,将过渡更改为不同的边缘(例如 .leading
)也是如此。
有没有其他人遇到过这样的事情?
确认在模拟器中描述的行为,在预览中一切正常。 Xcode 11.2。像下面这样在 VStack 中包装解决了这个问题。
var scrollView: some View {
VStack {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
Text("Horizontal list")
Text("Horizontal list")
Text("Horizontal list")
Text("Horizontal list")
}
}
.frame(height: 100)
}
.transition(.move(edge: .bottom))
}
您需要配置HStack。
struct ContentView: View {
@State private var showScroll: Bool = false
var body: some View {
VStack {
Spacer()
Button(action: {
withAnimation {
self.showScroll = true
}
}, label: {
Text("Hit me")
}).padding()
.background(Capsule().fill())
Spacer()
if showScroll {
scrollView
}
}
}
var scrollView: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
Text("Horizontal list")
Text("Horizontal list")
Text("Horizontal list")
Text("Horizontal list")
}.frame(maxHeight: .infinity)
}
.frame(height: 100)
.transition(.move(edge: .bottom))
}
}
使用 .frame(minHeight: 0, maxHeight: .greatestFiniteMagnitude)
而不是 .frame(maxHeight: .infinity)
给了我更好的结果。 IE 。我可以为 .frame(height: 100)
设置任何值,它不会冻结。