简体   繁体   English

SwiftUI TabView PageTabViewStyle 防止更改标签?

[英]SwiftUI TabView PageTabViewStyle prevent changing tab?

I have a TabView in SwiftUI in the PageViewTabStyle so i can swipe from page to page.我在PageViewTabStyle的 SwiftUI 中有一个TabView ,所以我可以从一页到另一页滑动。 I'd like to have a setting that "locks" the current view in place, so the user cannot swipe.我想要一个“锁定”当前视图的设置,因此用户无法滑动。 Googling and reading docs isn't turning up anything obvious for me, so I was hoping the gurus on SO could help me out.谷歌搜索和阅读文档对我来说没有任何明显的意义,所以我希望 SO 上的专家可以帮助我。

In short, my code looks like简而言之,我的代码看起来像

TabView {
   ForEach(0..<5) { idx in
      Text("Cell: \(idx)")
   }
}
.tabViewStyle(PageTabViewStyle())

I have found the disabled property, but then it appears that all tap events are ignored on the entire view - I just want to prevent the user from switching tabs (or, in this particular case, swiping or pressing the page dots to switch pages).我找到了disabled属性,但似乎在整个视图中所有点击事件都被忽略了 - 我只是想阻止用户切换选项卡(或者,在这种特殊情况下,滑动或按下页面点来切换页面) . I've tried the solution from here where the gesture property is set to nil , but that doesn't appear to actually stop the swipe gesture from changing the page (the indexDisplayMode bit was nice, though!)我已经尝试了从heregesture属性设置为nil的解决方案,但这似乎并没有真正阻止滑动手势更改页面(尽管indexDisplayMode位很好!)

Any help is greatly appreciated!任何帮助是极大的赞赏! Thanks!谢谢!

The solution from mentioned reference works, just the swipe is blocked not by gesture(nil) , but by gesture(DragGesture()) .来自提到的参考作品的解决方案,只是滑动不是被gesture(nil)阻止,而是被gesture(DragGesture())阻止。 And view should be full-tab-content-view-wide, like并且视图应该是全标签内容视图范围,例如

    TabView {
      ForEach(0..<5) { idx in
        Text("Cell: \(idx)")
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .contentShape(Rectangle())
                .gesture(DragGesture())      // this blocks swipe
      }
    }
    .tabViewStyle(PageTabViewStyle())

Tested with Xcode 12.1 / iOS 14.1使用 Xcode 12.1 / iOS 14.1 测试

* and, of course, it can be made conditional as in https://stackoverflow.com/a/63170431/12299030 *当然,它可以像https://stackoverflow.com/a/63170431/12299030

To block all the swipe gestures in a TabView you have to use .simultaneousGesture(DragGesture()) that blocks all the swipe gestures in the subviews as well要阻止 TabView 中的所有滑动手势,您必须使用.simultaneousGesture(DragGesture())来阻止子视图中的所有滑动手势

TabView {
          ForEach(0..<5) { idx in
            Text("Cell: \(idx)")
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .contentShape(Rectangle())
                    .simultaneousGesture(DragGesture())
          }
        }
        .tabViewStyle(PageTabViewStyle())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SwiftUI 具有基于内容的 PageTabViewStyle 动态高度的 TabView - SwiftUI TabView with PageTabViewStyle dynamic height based on Content SwiftUI:自定义 TabView 的 PageTabViewStyle 以显示标签而不是点 - SwiftUI: customizing TabView's PageTabViewStyle to show tags instead of dots SwiftUI TabView PageTabViewStyle 崩溃而不显示任何错误 - SwiftUI TabView PageTabViewStyle crashes without showing any error 如何在 SwiftUI 中使用 PageTabViewStyle 摆脱 TabView 上的索引点? - How can I get rid of index dots on TabView with PageTabViewStyle in SwiftUI? 使用 tabview 更改选项卡时,如何使 SwiftUI 中的计时器保持触发 - How to make a timer in SwiftUI keep firing when changing tab with tabview 带有 PageTabViewStyle 的 TabView 中的 Memory 泄漏 - Memory leak in TabView with PageTabViewStyle 带有 PageTabViewStyle 的 TabView 上的edgesIgnoringSafeArea 不起作用 - edgesIgnoringSafeArea on TabView with PageTabViewStyle not working "SwiftUI TabView 与 PageTabViewStyle 在设备上的横向与 safeArea 添加奇数前沿插图" - SwiftUI TabView with PageTabViewStyle in Landscape on device with safeArea adding odd leading edge inset SwiftUI:点击 TabView 的选项卡时崩溃 - SwiftUI: crash when tapping the TabView's tab 每个选项卡上具有不同 TabBar 背景颜色的 TabView SwiftUi - TabView with different TabBar backgroundColor on each Tab SwiftUi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM