Giter Club home page Giter Club logo

Comments (3)

frzi avatar frzi commented on May 30, 2024 1

Finally had a chance to give this a try. So far I experienced no problems getting the TabView to behave nicely.

Here's the entire contents for the ContentView. The only thing omitted here is the Router, which is wrapped around the ContentView in the App (inside the WindowGroup). This was tested on iOS 16.

import SwiftUI
import SwiftUIRouter

extension String: Identifiable {
	public var id: Self { self }
}

enum Constants {
	static let titles: [(title: String, image: String)] = [
		("Movies", "film"),
		("Music", "music.note"),
		("Books", "book"),
	]
}

struct ContentView: View {
	var body: some View {
		SwitchRoutes {
			Route("tabs/*") {
				TabsView()
			}

			Route {
				VStack {
					ForEach(Array(Constants.titles.enumerated()), id: \.element.title) { (index, element) in
						NavLink(to: "/tabs/" + element.title) {
							Text("Go to /tabs/" + element.title)
						}
					}
				}
			}
		}
	}
}

private struct TabsView: View {
	@EnvironmentObject private var navigator: Navigator
	@EnvironmentObject private var routeInformation: RouteInformation
	@State private var selected = -1

	private func setTabForRoute(_ route: String) {
		if let index = Constants.titles.firstIndex(where: { $0.title == route }) {
			selected = index
		}
	}

	var body: some View {
		TabView(selection: $selected) {
			ForEach(Array(Constants.titles.enumerated()), id: \.element.title) { (index, element) in
				VStack {
					Text(element.title)
					NavLink(to: "/") {
						Text("Back to root")
					}
				}
				.tag(index)
				.tabItem {
					Label(element.title, systemImage: element.image)
				}
			}
		}
		.onChange(of: selected) { newIndex in
			navigator.navigate(routeInformation.path + "/" + Constants.titles[newIndex].title)
		}
		.onChange(of: navigator.path) { newPath in
			let components = newPath.components(separatedBy: "/").dropFirst()
			if let route = components.first {
				setTabForRoute(route)
			}
		}
		.onAppear {
			let components = navigator.path[routeInformation.path.endIndex...].components(separatedBy: "/").dropFirst()
			if let route = components.first {
				setTabForRoute(route)
			}
		}
	}
}

The code was written hasty. Plenty of room for cleaning up 🙂

from swiftui-router.

frzi avatar frzi commented on May 30, 2024

The quickest I can do now is link you to the TabView example which uses a TabView with SwiftUI Router. It uses a TabView at the top level of the view hierarchy, though the logic for a TabView inside a router shouldn't be too different. 🙂

from swiftui-router.

IlyasNN avatar IlyasNN commented on May 30, 2024

I have seen this example. TabView is used as a root view here...
I want to create something like these:

IMG_5572
If I add TabView as second screen after empty root view for routing, I get empty tabView...
Any ideas how to solve the issue?

from swiftui-router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.