SwiftUI : navigation to next view from navigation bar button? - ios

I want to push a view when tapping the trailing button of the navigation bar, so I tried this:
struct ContentView: View {
var body: some View {
NavigationView {
MyListView()
.navigationBarTitle(Text("Main list title"))
.navigationBarItems(trailing: Button("Add", action: {
NavigationLink(destination: MyFormView()) {
Text("Hello!")
}
}))
}
}
}
But nothing happens when I tap the button and I end up having an issue on the NavigationLink line:
Result of 'NavigationLink<Label, Destination>' initializer is unused
Thank you for helping me

Related

NavigationBar with Custom Back Text but no NavigationBarTitle

I want to hide the navigationbarTitle for my views, but keep a custom value for the back button in the child views. So the NavigationBar should be visible in every screen, but should not have a title. But i want to change the text 'Back' to a custom text.
NavigationLink {
SomeChildView()
} label: {
SomeView()
}.navigationBarTitle("Text for back button in child view")
If I set the title on the NavigationLink this gives me my custom back button text but also displays the title in the Parent View.
You can achieve what you need by using the environment value of presentationMode to dismiss the screen you are in by code, and for changing the back button label and style you can simply do
hide the back button by using .navigationBarBackButtonHidden(true) modifier
use toolbar and toolbarItem to add your custom back button to the NavigationBar
here an example of how you can use it
// FirstScreen
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink {
SecondScreen()
} label: {
Text("Go To Second Screen")
}
}
}
}
// SecondScreen
struct SecondScreen: View {
#Environment(\.presentationMode) var presentationMode
var body: some View {
Text("Second Screen")
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button {
presentationMode.wrappedValue.dismiss()
} label: {
Label("Custom Back Button", systemImage: "command")
.labelStyle(.titleAndIcon)
}
}
}
}
}

how to hide navigation bar on particular screen in SwiftUI?

I am navigating from one screen to another on previous screen i am hiding navigation bar but on second screen I want to show navigation bar but in mu case its hiding on second screen also.
navigation bar hidden on first screen as
.navigationBarHidden(true)
How can i show navigation bar on second screen? (I am NOT using navigation view on either screen)
My First parent screen has
NavigationView {
//design
}
.navigationBarTitle("ABCScreen", displayMode: .inline)
.navigationViewStyle(StackNavigationViewStyle())
Thank you for help.
The possible approach to hide navigation bar in root view and show in child subviews.
struct FirstNavigationView: View {
#State private var hideBar = true // << hide state
var body: some View {
NavigationView {
VStack {
Text("FirstView")
Divider()
NavigationLink("Goto Child", destination: NextChildView(index: 1))
.simultaneousGesture(TapGesture().onEnded {
self.hideBar = false // << show
})
}
.navigationBarHidden(hideBar)
// .navigationBarTitle("Back to Root") // << choice
.onAppear {
self.hideBar = true // << hide on back
}
}
}
}
The only needed modifications is in root view.
For your first screen you can add .navigationBarHidden(true) inside your NavigationView to hide it and false on second screen to unhide.
FIRST SCREEN:
NavigationView{
Button(action: {}, label: {
Text("Button")
})
.navigationBarHidden(true) //This flag will hide your navigationBar
}
SECOND SCREEN:
NavigationView{
Button(action: {}, label: {
Text("Button")
})
.navigationBarHidden(false) //This flag will unhide your navigationBar
}

How to hide NavigationBar when the view disappears in SwiftUI?

struct SomeView: View {
var body: some View {
ZStack {
//rest of the code goes here
}.navigationBarTitle("Some View")
}
}
I am trying to hide my view's NavigationBar using .onDisappear{} but it doesn't work and throws a warning. How can I hide my NavigationBar when the view disappears or some condition returns true but not otherwise?
Hide navigation bar view - navigationBarHidden option
struct SomeView: View {
#State private var condition = false
var body: some View {
NavigationView {
Toggle(isOn: $condition) {
Text("Hide NavigationBar View")
}.padding(.horizontal)
.navigationBarTitle("Some View")
.navigationBarHidden(condition)
}
}
}

SwiftUI NavigationView Back Button does not show NavigationBarTitle

I have two views. View1 has a NavigationLink which leads to View2.
However the "Back" Button to get back to View1 only says "Back" and not the title of View1.
How can I change the Buttons Text without creating a button via NavigationBarItems?
Adding a Button via NavigationBarItems removes the animation when the second view shows up.
struct View1: View {
var body: some View {
NavigationView {
List {
Text("ExampleText1")
Text("ExampleText2")
}
.navigationBarTitle("View1")
.navigationBarItems(
trailing:
NavigationLink(destination: View2()) {
Image(systemName: "plus.circle").font(.system(size: 25))
})
}
}
}
struct View2: View {
var body: some View {
List {
Text("Example")
Text("Example")
}
.navigationBarTitle("View2")
}
}
Solution:
"View1" was too long.

SwiftUI Modal Sheet Re-Opening After Dismiss

I have a list in a Navigation View, with a trailing navigation button to add a list item. The button opens a modal sheet. When I dismiss the sheet (by pulling it down), the sheet pops right back up again automatically and I can't get back to the first screen. Here's my code.
struct ListView: View {
#ObservedObject var listVM: ListViewModel
#State var showNewItemView: Bool = false
init() {
self.listVM = ListViewModel()
}
var body: some View {
NavigationView {
List {
ForEach(listVM.items, id: \.dateCreated) { item in
HStack {
Text(item.name)
Spacer()
Image(systemName: "arrow.right")
}
}
}
.navigationBarTitle("List Name")
.navigationBarItems(trailing: AddNewItemBtn(isOn: $showNewItemView))
}
}
}
struct AddNewItemBtn: View {
#Binding var isOn: Bool
var body: some View {
Button(
action: { self.isOn.toggle() },
label: { Image(systemName: "plus.app") })
.sheet(
isPresented: self.$isOn,
content: { NewItemView() })
}
}
I am getting this error:
Warning: Attempt to present <_TtGC7SwiftUIP13$7fff2c603b7c22SheetHostingControllerVS_7AnyView_: 0x7fc5e0c1f8f0> on which is already presenting (null)
I've tried toggling the bool within "onDismiss" on the button itself, but that doesn't work either. Any ideas?
Turns out putting the button in the navigationBarItems(trailing:) modifier is the problem. I just put the button in the list itself instead of in the nav bar and it works perfectly fine. Must be some kind of bug.

Resources