How to change PageTabView programmatically in iOS 14, SwiftUI 2?

天大地大妈咪最大 提交于 2021-01-20 06:33:14

问题


I'm exploring new things came in Xcode 12 and SwiftUI 2.0

I have created a pageview onboarding screen with TabView and PageTabViewStyle in Xcode 12, iOS 14, Swift UI 2. I want to add the next button when clicking on it, the page should move to the second view. Here Text("Hello").

struct OnBoardingView: View {
    var body: some View {
        TabView {
            Text("Hi")
            Text("Hello")
            Text("Welcome")
        }
        .tabViewStyle(PageTabViewStyle())
        .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
    }
}

回答1:


Here is a demo of solution. Tested with Xcode 12 / iOS 14

struct OnBoardingView: View {
    @State private var selectedPage = 0
    var body: some View {
        VStack {
            HStack {
                Button("<") { if selectedPage > 0 {
                    withAnimation { selectedPage -= 1 }
                } }
                Spacer().frame(width: 40)
                Button(">") { if selectedPage < 2 {
                    withAnimation { selectedPage += 1 }
                } }
            }
            TabView(selection: $selectedPage) {
                Text("Hi").tag(0)
                Text("Hello").tag(1)
                Text("Welcome").tag(2)
            }
            .tabViewStyle(PageTabViewStyle())
            .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
        }
    }
}


来源:https://stackoverflow.com/questions/62833524/how-to-change-pagetabview-programmatically-in-ios-14-swiftui-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!