How to create a custom TabView with NavigationView in SwiftUI?

swift nub

[EDIT] - This question has been edited and simplified.

I need to create a CustomLooking TabView instead of the default one.

Here is my full code with the problem. Just run the code below.

import SwiftUI

enum TabName {
    case explore, network
}

struct ContentView: View {
    @State var displayedTab: TabName = .explore
    var body: some View {
        VStack(spacing: 0) {
            Spacer()
            switch displayedTab {
            case .explore: AViewWhichNavigates(title: "Explore").background(Color.yellow)
            case .network: AViewWhichNavigates(title: "Network").background(Color.green)
            }
            Spacer()
            CustomTabView(displayedTab: $displayedTab)
        }
    }
}

struct CustomTabView: View {
    @Binding var displayedTab: TabName
    var body: some View {
        HStack {
            Spacer()
            Text("Explore").border(Color.black, width: 1).onTapGesture { self.displayedTab = .explore }
            Spacer()
            Text("Network").border(Color.black, width: 1).onTapGesture { self.displayedTab = .network }
            Spacer()
        }
    }
}

struct AViewWhichNavigates: View {
    let title: String
    var body: some View {
        NavigationView(content: {
            NavigationLink(destination: Text("We are one level deep in navigation")) {
                Text("You are at root. Tap to navigate").navigationTitle(title)
            }
        })
    }
}

On tab#1 click the navigation. Switch to tab#2, then Switch back to tab#1. You will see that tab#1 has popped to root.

How do I prevent the customTabView from popping to root every time i switch tabs?

Just a coder

All you need is a ZStack with opacity.

import SwiftUI

enum TabName {
    case explore, network
}

struct ContentView: View {
    @State var displayedTab: TabName = .explore
    var body: some View {
        VStack {
            ZStack {
                AViewWhichNavigates(title: "Explore")
                    .background(Color.green)
                    .opacity(displayedTab == .explore ? 1 : 0)
                AViewWhichNavigates(title: "Network")
                    .background(Color.green)
                    .opacity(displayedTab == .network ? 1 : 0)
            }
            CustomTabView(displayedTab: $displayedTab)
        }
    }
}

struct CustomTabView: View {
    @Binding var displayedTab: TabName
    var body: some View {
        HStack {
            Spacer()
            Text("Explore").border(Color.black, width: 1).onTapGesture { self.displayedTab = .explore }
            Spacer()
            Text("Network").border(Color.black, width: 1).onTapGesture { self.displayedTab = .network }
            Spacer()
        }
    }
}

struct AViewWhichNavigates: View {
    let title: String
    var body: some View {
        NavigationView(content: {
            NavigationLink(destination: Text("We are one level deep in navigation")) {
                Text("You are at root. Tap to navigate").navigationTitle(title)
            }
        })
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SwiftUI TabView inside a NavigationView

SwiftUI - TabView with NavigationView generates gray area

SwiftUI: TabView and NavigationView play nicely on iPhone but not on iPad?

Swiftui: How to give custom pager tabView the right padding

SwiftUI - Overlay TabView with Custom View

SwiftUI - TabView Overlay with custom view

How to rebuild a NavigationView in SwiftUI

SwiftUI TabView + NavigationView navbar doesn't show up

NavigationView title doesn't appear when the views are in TabView in SwiftUI

NavigationView doesn't display correctly when using TabView in SwiftUI

Change background color of View inside TabView having NavigationView and ScrollView in SwiftUI

ios - TabView in SwiftUI loses background when content of the NavigationView is too short

SwiftUI, setting title to child views of TabView inside of NavigationView does not work

SwiftUI - Navigation bar title not displayed when nesting TabView in NavigationView

SwiftUI: how to create custom UIDatePicker

How to clear badge in TabView on SwiftUI

How to swap TabView items in SwiftUI?

How to make a colorful TabView in SwiftUI?

How to show a sheet with a NavigationView in SwiftUI

How to hide NavigationView Bar in SwiftUI

How to use NavigationView and NavigationLink in SwiftUI?

Creating a custom back button with NavigationView on SwiftUI

How to navigate from a subview of a TabView back to the TabView in SwiftUI?

UWP NavigationView and TabView collision

How to fix gray bar under List in NavigationView in TabView?

How can I create custom shapes in swiftUI

SwiftUI - How to create and use an array of custom types

How to create custom slider by using SwiftUI?

How to create a localizable custom struct? (SwiftUI)

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive