0

I have a paging style TabView in a HStack with another view. I'm adding a selection binding to the TabView to programmatically control selection.

When a tab is tapped the second view in the HStack disappears and the TabView resizes to fill the space.

However, if this is done after scrolling through my TabView's contents what happens is the selection changes many times as the size changes, causing the TabView to erratically scroll through many pages, rather than stay on the same selection and resize.

To reproduce:

  1. Create a new project
  2. Replace ContentView with the following code
  3. Scroll through ~20 pages
  4. Tap on the rectangle (blue or red)

The selection will change by itself.

Tested on iOS 14.5

struct ContentView: View {
    @State var showSidebar = true
    @State var selection = 0
    
    var body: some View {
        HStack {
            Rectangle()
                .frame(width: showSidebar ? 100 : 0)
                .frame(maxHeight: .infinity)
            
            TabView(selection: $selection) {
                ForEach(0..<100, id: \.self) { i in
                    Rectangle()
                        .foregroundColor(i % 2 == 0 ? .red : .blue)
                        .padding()
                        .onTapGesture {
                            withAnimation {
                                showSidebar.toggle()
                            }
                        }
                }
            }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
        }
    }
}
4

0 回答 0