在 swiftUI 中使用分页选项卡视图时如何阻止页面上下滚动

How to stop the page from being able to be scrolled up and down while using a paged tab view in swiftUI

我正在尝试构建一个在分页选项卡视图中显示一周中不同日期的应用程序,但是当我横向滚动到某一天(例如星期二)时,我可以上下滚动它,就好像它是滚动视图。我的内容视图中没有滚动视图。

我的代码是这样的:

struct ContentView: View {
    var body: some View {
        TabView {
            Text("Saturday")
            Text("Sunday")
            Text("Monday")
            Text("Tuesday")
            Text("Wednesday")
            Text("Thursday")
            Text("Friday")
        }
        .tabViewStyle(PageTabViewStyle())
    }
}

你可以这样做

TabView 放入 ScrollView.onAppear()

.onAppear(perform: {
            UIScrollView.appearance().alwaysBounceVertical = false
})  

struct ContentView: View {
    var body: some View {
        ScrollView(.vertical, showsIndicators: false) {
            TabView {
                Text("Saturday")
                Text("Sunday")
                Text("Monday")
                Text("Tuesday")
                Text("Wednesday")
                Text("Thursday")
                Text("Friday")
            }
            .tabViewStyle(PageTabViewStyle())
            .frame(width: 300, height: 600, alignment: .center)
        }
        .frame(width: 300, height: 600, alignment: .center)
        .background(Color.blue)
        .onAppear(perform: {
            UIScrollView.appearance().alwaysBounceVertical = false
        })
    }
}