iOS14 SwiftUI How to Disable Scroll on TabView

Viewed 2061
TabView(selection: $vm.selectedTab) {
   PlanCard_PositiveDay(vm: vm).tag(0)
   PlanCard_CountdownDay().tag(1)
   PlanCard_Basalkcal().tag(2)
   PlanCard_ActivityDiet().tag(3)
   PlanCard_Schedule().tag(4)
   PlanCard_WeightTarget().tag(5)
}
.tabViewStyle(PageTabViewStyle.init(indexDisplayMode: .never))

I use TabView with PageTabViewStyle to build a pageView, but I want to click some button to change position, not auto-scroll to change position with finger gesture, so I want to disable auto-scroll, How can I update my code To achieve this

Thx!

1 Answers

Use .contentShape modifier along with DragGesture():

TabView(selection: $vm.selectedTab) {

   PlanCard_PositiveDay(vm: vm).tag(0).contentShape(Rectangle()).gesture(DragGesture())

   PlanCard_CountdownDay().tag(1).contentShape(Rectangle()).gesture(DragGesture())

   PlanCard_Basalkcal().tag(2).contentShape(Rectangle()).gesture(DragGesture())

   PlanCard_ActivityDiet().tag(3).contentShape(Rectangle()).gesture(DragGesture())

   PlanCard_Schedule().tag(4).contentShape(Rectangle()).gesture(DragGesture())

   PlanCard_WeightTarget().tag(5).contentShape(Rectangle()).gesture(DragGesture())

}
.tabViewStyle(PageTabViewStyle.init(indexDisplayMode: .never))
Related