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

ScrollViewReader and scrollTo isssues.....possible bug?

Forums > SwiftUI

I've found that using scrollTo inside of an onChange closure doesn't scroll to the correct location if the row height of the List item is greater than a single row. It will scroll to the height of a single row leaving everything else in the row hidden. If I use the scrollTo as a Button action it will accurately read the height of the VStack and scroll to a position that shows the full row. I've also tried delaying the scrollTo action in the onChange event just in case the UI was still updating, but no luck with that either.

The second image is a result of clicking the button, the first is the triggering of the onChange event.

Here is the code I'm working with:

    Button("Scroll To ") {
        readerProxy?.scrollTo(careLogItems.last)
    }
    ScrollViewReader { reader in
        List(careLogItems) { i in
            VStack {
                Text(i.logText)
                Text(i.logDate.description)
            }.id(i)
        }.onAppear {
            readerProxy = reader
            //reader.scrollTo(careLogItems.last)
        }
        CareLogItemDetail(careLogItem: $newCareLogItem, addItem: addCareLogItem)
    }
}
.onChange(of: careLogItems) { _ in
    print("changed \(careLogItems.count)")
    readerProxy?.scrollTo(careLogItems.last)
}
.onAppear {
    if (hasAppeared == false) {
        hasAppeared = true
        loadCareLogItems()
    }
}

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

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

You are not logged in

Log in or create account
 

Link copied to your pasteboard.