SwiftUI - Add a Navigation Bar Button on Condition
As .navigationBarItems
will be deprecated in a future version of iOS it's better to use .toolbar(_:)
View Modifier.
.toolbar {
ToolbarItemGroup(placement: .navigationBarTrailing) {
if tabIndex == 2 {
// show button
}
}
}
Here is possible approach
.navigationBarItems(trailing: self.tabIndex == 1 ?
AnyView(self.trailingButton) : AnyView(EmptyView()))
somewhere below body
var trailingButton: some View {
Button(action: {print("Button was tapped")}) {
Image(systemName: "plus")
.resizable().frame(width: 20, height: 20)
}
}