1

I am working on an app that requires 6 items in a PageView, xcode was acting up so i came on Stack Overflow and found pawello2222's way, found it really helpful but I still need to connect it to the horizontal links on top. here is the code, thanks a million.

Original solution: How can I implement PageView in SwiftUI?

Below is my code and what I want to achieve, swiping left or right works, but clicking on the links does not change the view

struct TestingView: View {
    @State var selection = 0

    var body: some View {
        VStack {
            HStack(spacing: 10) {
                VStack {
                    Text("Link 1")
                        .foregroundColor(self.selection == 0 ? Color.blue : Color("Silver").opacity(0.7))
                        .font(.caption)
                        .fontWeight(.bold)
                        .clipShape(Rectangle())
                        .onTapGesture {
                            withAnimation(.default) {
                                self.selection = 0
                            }
                        }
                }

                Spacer(minLength: 0)

                VStack {
                    Text("Link 2")
                        .foregroundColor(self.selection == 1 ? Color.blue : Color("Silver").opacity(0.7))
                        .font(.caption)
                        .fontWeight(.bold)
                        .clipShape(Rectangle())
                        .onTapGesture {
                            withAnimation(.default) {
                                self.selection = 1
                            }
                        }
                }

                Spacer(minLength: 0)

                VStack {
                    Text("Link 3")
                        .foregroundColor(self.selection == 2 ? Color.blue : Color("Silver").opacity(0.7))
                        .font(.caption)
                        .fontWeight(.bold)
                        .clipShape(Rectangle())
                        .onTapGesture {
                            withAnimation(.default) {
                                self.selection = 2
                            }
                        }
                }
            }

            PageView(selection: $selection, indexDisplayMode: .never, indexBackgroundDisplayMode: .never) {
                VStack {
                    FirstView()
                }
                .tag(0)

                VStack {
                    SecondView()
                }
                .tag(1)

                VStack {
                    ThirdView()
                }
                .tag(2)
            }
        }
    }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
EMPIRE
  • 57
  • 7
  • I am trying out ways connecting his code with .onTapGesture { withAnimation(.default){ self.selection = 1 } } – EMPIRE Feb 04 '21 at 21:07
  • @pawello2222 Howdy, please your help is needed, thanks. – EMPIRE Feb 04 '21 at 22:06
  • Thanks for the info. In the original code I used two `Binding` properties due to a TabView bug. It seems that it's fixed now, so I updated my answer - please try it again. – pawello2222 Feb 04 '21 at 22:43
  • Howdy @pawello2222 thanks for the reply, i followed and took out one binding as you did, but when i ran the app it built in the simulator alright but crashed when i opened the page with the code, Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeea0abff8) – EMPIRE Feb 05 '21 at 04:26
  • I found the problem, you mistakenly removed the peroid (.) before tabViewStyle(PageTabViewStyle(indexDisplayMode: indexDisplayMode)) and i followed blindly. haha. thanks so much i really appreciate your help – EMPIRE Feb 05 '21 at 04:48

0 Answers0