macOS 13.0+, Apple introduce a new api named MenuBarExtra which can add a menubar in macOS. But I can't add a searchable protocol into the content to add a search view ?
@main
struct AppWithMenuBarExtra: App {
@AppStorage("showMenuBarExtra") private var showMenuBarExtra = true
var body: some Scene {
WindowGroup {
ContentView()
}
MenuBarExtra(
"App Menu Bar Extra", systemImage: "star",
isInserted: $showMenuBarExtra)
{
NavigationLink {
Text("Search")
}.searchable()
}
}
}
I expect a search bar can show in the menuBar
I have solved this problem, here is my solution:
We can not add a
NavigationLink
and its protocolsearchable
into aMenuBarExtra
scene for adding a search view, but we can add aTextField
as a searchBar inMenuBarExtra
when we set the modifier.menuBarExtraStyle
to.window
: