Toolbar Is Deleting My Back Button in the Navigationview

Toolbar is deleting my back Button in the NavigationView

import SwiftUI

struct ToolbarBug: View {
var body: some View {
NavigationView {
NavigationLink(destination: DetailViewToolbarBug()) {
Text("Unknown")
}
}
}
}
struct DetailViewToolbarBug: View {
var body: some View {
VStack{
NavigationLink(destination: DetailViewToolbarBug()) {
Text("Unknown")
}
Form {
NavigationLink(destination: DetailViewToolbarBug()) {
Text("Unknown")
}
Text("form text")
}
}
.navigationBarTitle("Deatil")
.toolbar {
ToolbarItem(placement: .primaryAction) {

Button(action: {

}) {
Image(systemName: "ellipsis.circle")
}
}
}
}
}
struct ToolbarBug_Previews: PreviewProvider {
static var previews: some View {
ToolbarBug()
}
}

SwiftUi Navigation Bar Button disappears after entering the third View (Controller)

I found the Problem!

The .toolbar modifier on the NavigationView hides the Backbutton in a Buggy way!

Remove back button text from navigationbar in SwiftUI

So I actually ended up with the following solution that actually works. I am overwriting the navigation bar items like so

.navigationBarItems(leading:
Image("backButton")
.foregroundColor(.blue)
.onTapGesture {
self.presentationMode.wrappedValue.dismiss()
}
)

The only issue with this was that the back gesture wasn't working so that was solved by actually extending the UINavigationController

extension UINavigationController: UIGestureRecognizerDelegate {
override open func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}

public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
}

Now it's looking exactly the way I want it, the solution is kinda hacky... but it works for now, hopefully SwiftUI will mature a little bit so this can be done easier.

Hide navigation bar but keep back button - SwiftUI

I seemed to have resolved my problem by following this and using @Environment

So instead of using a NavigationLink in my final tab like this:

ZStack(alignment: .topLeading) {
Text("Tab1View")
NavigationLink(destination: ProductList()){
Image(systemName: "chevron.backward")
}
}

I am now using a button that dismisses the view like this using @Environment:

struct Tab1View: View {

@Environment(\.presentationMode) var presentation

var product: ProductModel
var body: some View {
ZStack(alignment: .topLeading) {
Text("Tab1View")
Button(action: {
self.presentation.wrappedValue.dismiss()
}, label: {
Text("PressMe")
})
}
}
}


Doing this allows me to hide the NavigationBar in the TabView the same way:

.navigationBarTitle("")
.navigationBarHidden(true)

Navigating back to main menu View in SwiftUI with Alert Button

To achieve what you are expecting, you actually need to add the NavigationView inside ContentView, if that's your main view. Because you navigate from ContentView to GameView, and what you are asking here is how to navigate back.

Applying the concept above, you can just dismiss GameView to go back to the in view.

Here is a sample code to achieve that:

Example of main view:

struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink {
GameView()

// This is how you hide the "<Back" button, so the user
// can navigate back only when tapping the alert
.navigationBarHidden(true)
} label: {
Text("Go to game view")
}
}
}
}

Example of game view:

struct GameView: View {

// This variable will dismiss the view
@Environment(\.presentationMode) var presentationMode

@State private var questionCounter = 1
@State var userAnswer = ""
@State private var alertTitle = ""
@State private var gameOver = false

// No need to use this variable
// @State private var menuNavigation = false

var body: some View {

// No need to have a NavigationView
// NavigationView {

// ZStack has no function apparently...
// ZStack{

// No need to have a NavigationLink
// NavigationLink(destination:ContentView(), isActive: $menuNavigation){
// Text("")
//}
VStack {

Text("Give the answer")
TextField("Give it", text: $userAnswer)

Button("Submit", action: answerQuestion)
}
.alert(isPresented: $gameOver) {
Alert(title: Text(alertTitle),
dismissButton: Alert.Button.default(
Text("Back to menu"), action: {

// This is how you go back to ContentView
presentationMode.wrappedValue.dismiss()
}
)
)
}
}


func answerQuestion() {

questionCounter += 1

if questionCounter == 2 {
gameOver.toggle()
alertTitle = "Game Over"
}
}
}


Related Topics



Leave a reply



Submit