I have a Mac Catalyst app I had built using SwiftUI,and I cant seem to add buttons to the trailing navigation bar?
I am also unsure where this navigationBar is defined, is it possible to remove? It only seems to have appeared in Ventura.
struct AppSidebarNavigation: View {
enum NavigationItem {
case home
}
@State private var selection: NavigationItem? = .home
init() {
#if !targetEnvironment(macCatalyst)
UITableView.appearance().backgroundColor = UIColor(named: "White")
UITableViewCell.appearance().selectionStyle = .none
UITableView.appearance().allowsSelection = false
#endif
}
var body: some View {
NavigationView {
sidebar
.navigationTitle("")
.navigationBarTitleDisplayMode(.inline)
// Main View
HomeView()
.navigationTitle("")
.edgesIgnoringSafeArea(.top)
.navigationBarHidden(isMac ? false : true)
.navigationBarBackButtonHidden(isMac ? false : true)
}
.accentColor(Color("Black"))
.navigationViewStyle(.columns)
}
}
HomeView I had the following to the View.
#if targetEnvironment(macCatalyst)
.navigationBarItems(trailing:
NavButtons
)
#endif
var NavButtons: some View {
HStack {
Button(action: {
Print("No")
}) {
Image(systemName: "plus")
.font(.system(size: 14, weight: .medium))
}
.buttonStyle(NavPlusButton())
}
}
2
Answers
I don’t think it is possible to do that because:
This indicates on macOS the function in not available.
I slightly modified your code (to get it to compile) and saw that where it would be is at the red circle below:
My code is:
You can use
Toolbar
modifier with.primaryAction
as placement to place button on the trailing side onNavigation Bar
.Just replace
.navigationBarItem
with below: