iOS Development

ios – Show desired tab from father or mother view of the TabView

Spread the love


I’ve a scrollable tabview that shows on the clicking of both button within the house view (ContentView). I need the first tab to point out when “View 1” is clicked, and the 2nd tab to point out when “View 2” is clicked. As soon as the tabview is opened, it ought to nonetheless perform correctly, that means the tab buttons and scrolling nonetheless works appropriately. Now that I give it some thought, that is much like clicking on the “Followers” or “Following” button on Instagram. If anybody has any concept on methods to clear up this, please let me know. Thanks!

ContentView.swift:

import SwiftUI

struct ContentView: View {
    var physique: some View {
        NavigationStack {
            HStack {
                NavigationLink {
                    ScrollTab(currentTab: 0)
                } label: {
                    Textual content("View 1")
                        .font(.subheadline)
                        .fontWeight(.semibold)
                }
                
                NavigationLink {
                    ScrollTab(currentTab: 1)
                } label: {
                    VStack {
                        Textual content("View 2")
                            .font(.subheadline)
                            .fontWeight(.semibold)
                    }
                    .body(width: 76)
                }
            }
        }
    }
}

ScrollTab.swift:

struct ScrollTab: View {
    @State var currentTab: Int = 0
    
    init(currentTab: Int) {
        self.currentTab = currentTab
    }
    
    var physique: some View {
        Spacer()
        VStack(alignment: .middle, spacing: 0) {
            TabBarView(currentTab: self.$currentTab)
            
            TabView(choice: self.$currentTab) {
                Textual content("That is view 1").tag(0)
                    .onAppear() {
                        self.currentTab = 0
                    }
                Textual content("That is view 2").tag(1)
                    .onAppear() {
                        self.currentTab = 1
                    }
            }
            .tabViewStyle(.web page(indexDisplayMode: .by no means))
            .background(Shade.secondary)
        }
        .edgesIgnoringSafeArea(.all)
    }
}

struct TabBarView: View {
    @Binding var currentTab: Int
    @Namespace var namespace
    
    var tabBarOptions: [String] = ["View 1", "View 2"]
    var physique: some View {
        HStack(spacing: 20) {
            ForEach(Array(zip(self.tabBarOptions.indices,
                              self.tabBarOptions)),
                    id: .0,
                    content material: {
                index, identify in
                TabBarItem(currentTab: self.$currentTab,
                           namespace: namespace.self,
                           tabBarItemName: identify,
                           tab: index)
                
            })
        }
        .padding(.horizontal)
        .background(Shade.orange) // <<<< Take away
        .body(top: 80)
    }
}

struct TabBarItem: View {
    @Binding var currentTab: Int
    let namespace: Namespace.ID
    
    var tabBarItemName: String
    var tab: Int
    
    var physique: some View {
        Button {
            self.currentTab = tab
        } label: {
            VStack {
                Spacer()
                Textual content(tabBarItemName)
                if currentTab == tab {
                    Shade.black
                        .body(top: 2)
                        .matchedGeometryEffect(id: "underline",
                                               in: namespace,
                                               properties: .body)
                } else {
                    Shade.clear.body(top: 2)
                }
            }
            .animation(.spring(), worth: self.currentTab)
        }
        .buttonStyle(.plain)
    }
}

I attempted altering the “currentTab” variable from a @State variable to a @Binding variable, however bumped into additional points, just like the tab buttons not working. Maybe making my very own tab bar could be the way in which to go. If anybody has options, please lmk.

One thought on “ios – Show desired tab from father or mother view of the TabView

Leave a Reply

Your email address will not be published. Required fields are marked *