Swiftui - Navigation View Opening with Back Button and Half Grey Screen/Weird Behavior

SwiftUI - Navigation View opening with Back button and half grey screen / weird behavior

You want to apply StackNavigationViewStyle to your NavigationView.

NavigationView {

...

}.navigationViewStyle(StackNavigationViewStyle())

You're experiencing an unwanted split view, and you can find more info here.

For larger devices like an iPad or iPhone Pro Max in landscape, it defaults to DoubleColumnNavigationViewStyle.

My view appears in back button of iPad but appears okay on iPhone

Are you using a NavigationView? By default those layout differently on iPad and iPhone. If that's the case, try adding this modifier to your NavigationView:

.navigationViewStyle(StackNavigationViewStyle())

Navigation Link leading to gray screen in Swift UI

You should only have one NavigationView in the view hierarchy.

Try creating one NavigationView at the root level:

struct ContentView: View {
var body: some View {
NavigationView {
PageOne()
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
}

and then remove NavigationView from subviews:

struct PageOne: View {
var body: some View {
VStack {
Text("This is page 1")
.font(.system(size: 36, weight: .bold))
.padding(.bottom)

NavigationLink(
destination: PageTwo(),
label: {
VStack {
Text("Go to Page 2")
.font(.system(size: 24, weight: .medium))
.foregroundColor(.white)
.frame(width: 200, height: 50, alignment: .center)
.background(Color.blue)
.cornerRadius(12)

}
})
}
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
struct PageTwo: View {
var body: some View {
VStack {
Text("This is page 2")
.font(.system(size: 36, weight: .bold))
.padding(.bottom)

NavigationLink(
destination: PageOne(),
label: {
VStack {
Text("Go to Page 1")
.font(.system(size: 24, weight: .medium))
.foregroundColor(.white)
.frame(width: 200, height: 50, alignment: .center)
.background(Color.blue)
.cornerRadius(12)

}
})
}
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}

Combine NavigationTitle and Navigation Back Button SwiftUI

jnpdx solved this... the solution was that I had an extra NavigationView in my SettingsView. I only needed one NavigationView.

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"
}
}
}

SwiftUI Is there a way to override the default back button in the navigation view toolbar without needing to add a modifier to every secondary view?

Unfortunately In SwiftUI there is not. You could however, override UINavigationController but it is not recommended as APIs can change.

Look at List for example, we used to set the UITableView appearance background color to .clear to customize List's background, but in iOS 16 this solution works no more.

Extended View and create a func where you put your code in it, then use that function wherever you need!

SwiftUI bug with navigation view rotation?

Changing ColumnNavigationViewStyle to StackNavigationViewStyle will solve your problem, the sequence you mentioned is most probably a bug, hopefully apple will solve it soon.

struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink("link") {
Text("hi")
}
}
.navigationViewStyle(.stack) //Style
}
}


Related Topics



Leave a reply



Submit