NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Horizontal ScrollView locks up app when translated off-screen

Forums > SwiftUI

Hi all, I've found a strange behavior that seems to be specific to ScrollViews when the axis is set to .horizontal - the issue doesn't repro with .vertical, nor does it repro for UIScrollViews.

When a parent is translated/offset/positioned such that a child horizontal scrollview is outside the screen's bounds, the app will lock up without any crash or errors in the console.

I'm absolutely baffled, can anyone explain why this happens? Here's a code snippet that repros the issue.

let timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
@State var offset: CGFloat = 0
var body: some View {
    VStack {
        ScrollView(.horizontal) {
            HStack {
                ForEach(0...10, id: \.self) { i in
                    Rectangle().fill(Color.red).frame(width: 50, height: 50)
                }
            }
        }
    }
    .offset(y: self.offset)
    .onReceive(self.timer, perform: { t in
        print("current offset: \(self.offset)")
        self.offset -= 5
    })
}

iOS version: 13.5 Xcode version: 11.5

--

On another note, if the scrollview is set to .vertical, I see the content jump around weirdly when it nears the top. Does anyone know why the layout system does this?

   

Subscribe to Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.