简体   繁体   中英

SwiftUI - Horizontal ScrollView locks up when transitioning from the bottom

Essentially, whenever I attempt to transition a horizontally scrolling ScrollView using AnyTransition.move(edge: .bottom) the app freezes, and memory keeps going up. I've managed to reproduce the issue in the following:

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))
    }
}

Changing the ScrollView axis to .vertical prevents the app from hanging, as does changing the transition to a different edge (eg .leading ).

Has anyone else come across anything like this?

You need to configure 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))
    }
}

Using .frame(minHeight: 0, maxHeight: .greatestFiniteMagnitude) instead of .frame(maxHeight: .infinity) gave me better results. ie . I could set any value for .frame(height: 100) and it would not freeze.

Confirm described behaviour in Simulator, in Preview all is ok. Xcode 11.2. Wrapping in VStack like below fixes the issue.

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))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM