问题描述
我在我的应用程序中设置了一个 TabView,这样我就可以在多个页面之间水平滑动,但我也有一个可能会出现的不需要的垂直滚动,具有反弹效果.如何禁用此垂直滚动?
我的代码:
结构内容视图:查看{@State 私有变量 currentTabIndex: Double = 0var主体:一些视图{虚拟堆栈{TabView(选择:$currentTabIndex){文本(文本 n°1").tag(0)文本(文本 n°2").tag(1)}.border(颜色.黑色).tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))}}}我遇到了同样的问题.这不是一个确切的解决方案,但您可以关闭滚动视图(在 TabView 中使用)上的弹跳.只要 TabView 中的项目不大于 TabView 框架,它就应该像禁用垂直滚动一样.
我会在 .onAppear 或您的 init 函数中调用它:
.onAppear(执行:{UIScrollView.appearance().bounces = false})
注意:这会禁用应用中所有滚动视图的弹跳...因此您可能需要重新启用它 .onDisappear.
I have set up a TabView in my application, so that I can swipe horizontally between multiple pages, but I also have an unwanted vertical scroll that may appear, with a bounce effect so. How can I disable this vertical scroll?
My code:
struct ContentView: View {
@State private var currentTabIndex: Double = 0
var body: some View {
VStack {
TabView(selection: $currentTabIndex) {
Text("Text n°1")
.tag(0)
Text("Text n°2")
.tag(1)
}
.border(Color.black)
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
}
}
}
I had this same problem. It's not an exact solution, but you can turn off bouncing on scrollviews (which is used within a TabView). And as long as the items within the TabView are not larger than the TabView frame, it should act as if you disabled vertical scrolling.
I would call it either .onAppear or in your init function:
.onAppear(perform: {
UIScrollView.appearance().bounces = false
})
Note: this disables the bouncing on ALL scrollviews across your app... So you may want to re-enable it .onDisappear.
这篇关于如何使用 SwiftUI 在 TabView 中禁用垂直滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!