0

I have horizontal menu scrollView inside Parent vertical scrollView. When I select menu, it refresh it refreshes listView. But to fix parent scrollView content size, I need to refresh entire View (scrollView) by assigning menuItem selected id to parent scrollView.

Everything works as expected but as I have assigned menuItem id to parent scrollView to fix content size/height issue, animation for scrolling selected menu doesn't work.

Is there any way I can achieve this animation?

struct SampleScrollView: View {

// Parent scroll view to fit multiple sub-views
ScrollView(.vetical, showsIndicators: false) {
 Vstack {
       HStack {
         ScrollView(.horizontal, showsIndicators: false) {
            ScrollViewReader { proxy in
                HStack {
                   ForEach(Menus, id: \.menuItem) { menu in
                      Button(action: { 
                          // This animation is not working as parent scrollView gets assigned with this selected menuItem id on menu click which helps Parent ScrollView to get refreshed so parent scrollview content size get's refreshed based on content height of result list view.
                          withAnimation {
                             proxy.scrollTo(menuItem, anchor: .leading)
                          }
                          
                          LoadList() 
                      }) {
                        Text("Option \(menuItem)")
                      }
                   }
                }
            }
         }
      }

      VStack {
         ScrollView(.vertical, showsIndicators: false) {
            LazyVStack {
               ForEach(resultArray, id: \.self) { item in
                 Text(item.title)
               }
            }
         }
      }
  
      // Some other views here
      VStack {  }

  }.id(menuItem) // Parent ScrollView Id get's updated every time when menu button selected
}

}
Vishwanath Deshmukh
  • 619
  • 1
  • 10
  • 32
  • the `id` force rebuild everything, so it appears wrong tool here. – Asperi Feb 15 '22 at 16:44
  • @Asperi What could be the best approach to achieve scrollable page with multiple sub-views? That's true `id` force rebuild entire page but without that parent ScrollView doesn't refresh it's content height on refreshing only child ListView (assigning id to child list view). – Vishwanath Deshmukh Feb 15 '22 at 20:04

0 Answers0