Learn Courses My Dashboard

Add a file to content.view

Alright so, i’m making a recipes app.

In this recipes app i have a navigation in form of a tabBar. In the different categories on the tabBar, etc “home, meat, vegetables, recipes”
I want different coding in these pages. So i notice when i edit the contentview.swift code it changes the app.

Here’s the code for the contentview.swift

import SwiftUI

   struct ContentView: View {
  @State private var selection = 0
   var body: some View {
    VStack {
        TabView(selection: $selection) {
            Text("Sök Recept")
            Text("Mina Recept")

    .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
      TabBarView(selection: $selection)
      struct ContentView_Previews:   
      PreviewProvider {
       static var previews: some View {

So i don’t really wanna change anything here cause i wanna create different file for each different tabBar option. How do
I go from there?

Also, here is my code for the TabBar itself.

import SwiftUI

struct TabBarView: View {
@Binding var selection: Int
@Namespace private var currentTab

var body: some View {
    HStack(alignment: .bottom)  {
        ForEach(tabs.indices) { index in
            GeometryReader { geometry in
               VStack(spacing: 4) {
            if selection == index {
                    .frame(height: 2)
                    .offset(y: -8)
                    .matchedGeometryEffect(id: "currentTab", in: currentTab) }
               if tabs[selection].label == "Sparade Recept" && tabs[index].label == "Sparade Recept" {
                    Image(systemName: tabs[index].image)
                        .frame(height: 20)
                } else {
                        .frame(height: 20)
            .fixedSize(horizontal: false, vertical: true)
            .frame(width: geometry.size.width / 2, height: 44, alignment: .bottom)
            .foregroundColor(selection == index ? Color(.label) : .secondary)
               .onTapGesture {
                withAnimation {
                    selection = index
            .frame(height: 44, alignment: .bottom)
            struct TabBarView_Previews: PreviewProvider {
static var previews: some View {
    TabBarView(selection: Binding.constant(0))
         struct Tab 
let image: String
let label: String }

let tabs = [
Tab(image: "house", label: "Startsida"),
Tab(image: "hare", label: "Animaliskt"),
Tab(image: "magnifyingglass", label: "Sök Recept"),
Tab(image: "leaf", label: "Vegetariskt"),
Tab(image: "books.vertical.fill", label: "Mina Recept")