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

Segmented picker state change resets position of scrollviews to top

Forums > SwiftUI

Hi,

Im working on a feature with a segmented picker and scrollviews under the picker.

I got the basics of the design implemented, however the the scrolled states resets everytime i tab the segmented button. I want to keep the state of the scrollviews so they dont scroll to the top when switched between the tabs...

I think this problem can be related to SwiftUI recomputing the view everytime the "type" variable is changed, however I could really like to prevent this behavior

Check it out here: https://streamable.com/alwlyh

import SwiftUI

public struct SegmentedPicker: View {

enum SegmentedType {
    case type1
    case type2
}

@State private var type: SegmentedType = .type1

public init() {}

public var body: some View {
    return NavigationView {
        VStack(alignment: .leading, spacing: 0) {
            VStack {
                Picker(
                    "",
                    selection: $type
                ) {
                    Text("Type1").tag(SegmentedType.type1)
                    Text("Type2").tag(SegmentedType.type2)
                }
                .pickerStyle(.segmented)
            }
            .padding(20)

            switch type {

            case .type1:
                ScrollView {
                    LazyVStack(spacing: 20) {
                        ForEach(0..<50) {
                            Text($0.description)
                        }
                    }
                    .padding()
                    .frame(maxWidth: .infinity)
                }
            case .type2:
                ScrollView {
                    LazyVStack(spacing: 20) {
                        ForEach(0..<50) {
                            Text($0.description)
                        }
                    }
                    .padding()
                    .frame(maxWidth: .infinity)
                }
            }
        }
        .navigationTitle("Segmented picker")
    }
}

}

struct SegmentedPicker_Previews: PreviewProvider { static var previews: some View { SegmentedPicker() } }

1      

@Bnerd  

I guess you want to change types but the scrollview to remain at the old position once you go back correct? If yes, you could try to observe the index position of the scrollView and once you navigate back it will scroll to the position you were left. Get ideas from this post : https://developer.apple.com/forums/thread/650312

1      

Thanks for the reply @Bnerd.

Exactly! I want to remain the old position when i switch between the tabs.

I'll check out the link and come back if I manage to make a solution using ScrollViewReader and GeometryReader.

1      

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.