Comments (3)
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.
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.
I have seen this example. TabView is used as a root view here...
I want to create something like these:
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)
- Nested routes without parent being rerendered on child navigation HOT 2
- How to use? HOT 2
- Set path on app launch HOT 2
- view init twice when navigate
- How to navigate to any page HOT 2
- Navigate navigates into void sometimes HOT 3
- Routes with missing optional parameters do not work without a trailing slash (/) HOT 3
- Flexible nested navigation HOT 2
- SwiftUIRouter using NavigationLink HOT 6
- Can't revisit a page HOT 2
- Full screen navigation without ignoring safe area
- Type safe routes HOT 4
- State of views from the back stack is not saved
- Able to switch to another tab then push to a certain view under navigation view then finally present a modal? HOT 1
- Full screen navigation above tabView HOT 4
- `.opacity` transition not working HOT 2
- Keep paths loaded in memory for performance (e.g. in tab bar) HOT 2
- xcodebuild error HOT 2
- Navigating to a blank screen issue HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftui-router.