[英]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!)我已经尝试了从here将gesture
属性设置为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.