1

So, for this app I need to make a custom styled tabview and I've started with the code below

import SwiftUI

enum Tab{
    case first
    case second
}

struct ContentView: View {
    @State private var selectedTab: Tab = .first
    var body: some View {
        VStack{
            switch selectedTab {
            case .first:
                NavigationView{
                    First()
                }
            case .second:
                NavigationView{
                    Second()
                }
            }
            
            
            
            CustomTabView(selectedTab: $selectedTab)
        }
    }
}

struct CustomTabView: View{
    @Binding var selectedTab: Tab
    var body: some View {
        HStack{
            Button {
                selectedTab = .first
            } label:{
                VStack{
                Image(systemName: "film")
                    .resizable()
                    .scaledToFit()
                    .frame(width:25,height: 25)
                
                Text("First")
                    .foregroundColor(.primary)
                    
                    
                }
                
            }
            Button {
                selectedTab = .second
            } label:{
                VStack{
                Image(systemName: "house")
                    .resizable()
                    .scaledToFit()
                    .frame(width:25,height: 25)
                
                Text("Second")
                    .foregroundColor(.primary)
                }
            }
            
        }
    }
}

struct First: View{
    var body: some View{
        Color(.systemGray6).ignoresSafeArea()
        navigationTitle("First")
    }
}

struct Second: View{
    var body: some View{
        Color(.systemGray4).ignoresSafeArea()
        navigationTitle("Second")
    }
}

While compiling the code and starting the simulation to see how it looks i get the loop error Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee1f2aff8) I tried renaming classes in hopes that they might overlap and cases, but from my perspective I really can't figure out why it's returning the loop error since I've already done custom tabviews in other apps. Help would be really appreciated. Thank you!

aheze
  • 24,434
  • 8
  • 68
  • 125
saxonsxexe
  • 97
  • 5

2 Answers2

1

You are missing a . before the modifier. Replace

navigationTitle("Second")

with

.navigationTitle("Second")

Also do this for navigationTitle("First").

aheze
  • 24,434
  • 8
  • 68
  • 125
0

You should use . in First and Second, also you need to use just one NavigationView and ZStack in ContentView

I updated your code that you do NOT need First and Second Views, you need just ContentView and CustomTabView


struct ContentView: View {
    
    @State private var selectedTab: Tab = .first
    
    var body: some View {
        
        NavigationView{     // <<: Here
            
            ZStack {        // <<: Here

                Color(selectedTab == .first ? .systemGray6 : .systemGray4).ignoresSafeArea() // <<: Here

                CustomTabView(selectedTab: $selectedTab)
                
            }
            .navigationTitle( selectedTab == .first ? "First" : "Second") // <<: Here
            .animation(.easeInOut, value: selectedTab) // <<: Here
  
        }
    }
}
ios coder
  • 1
  • 4
  • 31
  • 91