Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested ScrollView stops scrollTo function #13

Open
ThushanS opened this issue May 10, 2021 · 5 comments
Open

Nested ScrollView stops scrollTo function #13

ThushanS opened this issue May 10, 2021 · 5 comments

Comments

@ThushanS
Copy link

iOS 13, When we enabled the horizontal scrollview(Test View) inside the main scrollview, scrollTo function doesn't working.
Looking some solution to put horizontal scrollview inside the vertical scrollview.
Thank you.

var body: some View {
    VStack(alignment: .leading) {
        //MARK:- Top horizontal category list
        HStack(spacing: 30) {
            ForEach(categories, id: \.self) { category in
                Button(action: {
                    if let proxy = proxy {
                        proxy.scrollTo(category, alignment: .top, animated: true)
                    }
                }) {
                    Text(category)
                        .fixedSize(horizontal: true, vertical: true)
                }
            }
        }
        .padding(.horizontal)
        
        //MARK:- Main vertical ScrollView
        ScrollView(.vertical, showsIndicators: false) { proxy in
            VStack(alignment: .leading, spacing: 30) {
                
                //MARK:- Horizontal ScrollView
                ScrollView(.horizontal, showsIndicators: false) {
                //When we enabled the horizontal scrollview here, scroll to function doesn't working.
                    HStack{
                        Text("Test view")
                            .font(.title)
                            .bold()
                    }
                }
                
                //MARK:- Main " Subviews
                ForEach(categories, id: \.self) { category in
                    VStack(alignment: .leading) {
                        Text(category)
                            .font(.largeTitle)
                            .foregroundColor(Color.black)
                            .onTapGesture {
                                proxy.scrollTo(category)
                            }
                        VStack(alignment: .leading) {
                            ForEach(0..<5) { index in
                                Text("Subview \(index)")
                                    .padding()
                            }
                        }.padding(.top)
                    }
                    .scrollId(category)
                }
            }
            .onAppear { self.proxy = proxy }
        }
    }
    .padding(.horizontal)
}
@Amzd
Copy link
Owner

Amzd commented May 10, 2021

Can you try with ScrollViewProxy around the vertical ScrollView rather than using the convenience init?

If you have type collision issues due to SwiftUI 2.0 you can use AmzdScrollViewProxy

@ThushanS
Copy link
Author

That's worked. Thank you. 👌

@Amzd
Copy link
Owner

Amzd commented May 15, 2021

Reopened because ScrollViewReader has to be inside the ScrollView to get the correct location of tagged views.

I suspect the issue is introspectScrollView returning the wrong UIScrollView when you have nested ScrollViews.

@Amzd Amzd reopened this May 15, 2021
@Amzd
Copy link
Owner

Amzd commented May 15, 2021

Hey, can you please confirm this issue because when I copied your original code to test it just worked.

@ThushanS
Copy link
Author

Appreciate your support. Issue #14 solved by moving the ScrollViewReader inside the ScrollView. But issue #13 started again. When I active the second scrollView (Horizontal) inside the main scrollView, the scrollTo function stoped.
Here is my code,

 struct ScrollViewTest: View {
    @State var categories: [String] = ["Cat One", "Cat Two", "Cat Three", "Cat Four"]
    @State private var proxy: ScrollViewProxy? = nil
    @State private var currentCategory: String?
    
    var body: some View {
        VStack(alignment: .leading) {
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 30) {
                    ForEach(categories, id: \.self) { category in
                        Button(action: {
                            DispatchQueue.main.async {
                                currentCategory = category
                            }
                            if let proxy = proxy {
                                DispatchQueue.main.async {
                                    proxy.scrollTo(category, alignment: .top, animated: true)
                                }
                            }
                        }) {
                            Text(category)
                                .foregroundColor(currentCategory == category ? Color.black : Color.blue)
                                .fixedSize(horizontal: true, vertical: true)
                        }
                    }
                }
                .padding(.horizontal)
            }
            
            ScrollView(.vertical, showsIndicators: false) {
                ScrollViewReader { proxy in
                    VStack(alignment: .leading, spacing: 30) {
                        //ScrollView(.horizontal, showsIndicators: false) {
                        //  HStack {
                        //      Text("Horizontal Views")
                        //  }
                        //}
                        ForEach(categories, id: \.self) { category in
                            VStack(alignment: .leading) {
                                Text(category)
                                    .font(.largeTitle)
                                    .foregroundColor(Color.black)
                                VStack(alignment: .leading) {
                                    ForEach(0..<12) { index in
                                        Text("Subview \(index)")
                                            .padding()
                                    }
                                }.padding(.top)
                            }
                            .scrollId(category)
                        }
                    }
                    .padding(.horizontal)
                    .onAppear { self.proxy = proxy }
                }
            }
        }
        .padding(.horizontal)
    }
    
}

@Amzd Amzd changed the title Horizontal scrollview stops scrollTo function Nested ScrollView stops scrollTo function May 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants