There is a huge space in my subview navigation bar.
I was assuming by adding trailing
buttons, it would align everything nicely to the right of the back
button.
This is my main view:
Now this is my subview:
Look at the huge gap at the top. I want the plus
button to be to the right of the back
button. Do I need to just create a custom back button for this or what?
Here is my code for the subview:
var body: some View {
NavigationView {
List {
Text("hello world")
Text("hello world")
Text("hello world")
}
.navigationBarTitle(todoList.title!)
.navigationBarItems(trailing:
HStack {
Button(action: {
self.add = true
}, label: {
Image(systemName: "plus")
})
}
)
}
}
I also want to remove the text from the back
button so it's just an image.
To summarize:
plus
button at the top to the right of the back
buttonback
button text, which reads ColorTodo
in this exampleIs there a SwiftUI native way of doing this or do I need a custom back button and to disable the default one?
From Settings, tap Display, and then tap Navigation bar. Make sure Buttons is selected, and then you can choose your desired button setup at the bottom of the screen. Note: This option will also affect the location you swipe when using Swipe gestures.
The reason for the extra space is that you are wrapping a NavigationView
inside a NavigationView
; remove the one inside your subview, and the plus button will be at the right height.
As for removing the text, yes, you would need to hide the default back button and replace it with your own. Subview might look something like
struct SubView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
List {
Text("hello world")
Text("hello world")
Text("hello world")
}
.navigationBarTitle(todoList.title!, displayMode: .inline) // 1
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: backButton, trailing: addButton)
}
var backButton: some View {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}, label: {
HStack {
Image(systemName: "chevron.left")
Text("Back") // 2
}
})
}
var addButton: some View {
Button(action: {
self.add = true
}, label: {
ZStack(alignment: .trailing) {
Rectangle() // 3
.fill(Color.red.opacity(0.0001)) // 4
.frame(width: 40, height: 40)
Image(systemName: "plus")
}
})
}
}
Notes:
displayMode: .inline
is not necessary, the default large title style looks a bit strange animating in and out.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With