UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.