func applicationDidFinishLaunching(_ aNotification: Notification) {
// SwiftUI content view & a hosting view
// Don't forget to set the frame, otherwise it won't be shown.
//
let contentViewSwiftUI = VStack {
Color.blue
Text("Test Text")
Color.white
}
let contentView = NSHostingView(rootView: contentViewSwiftUI)
contentView.frame = NSRect(x: 0, y: 0, width: 200, height: 200)
// Status bar icon SwiftUI view & a hosting view.
//
let iconSwiftUI = ZStack(alignment:.center) {
Rectangle()
.fill(Color.green)
.cornerRadius(5)
.padding(2)
Text("3")
.background(
Circle()
.fill(Color.blue)
.frame(width: 15, height: 15)
)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomTrailing)
.padding(.trailing, 5)
}
let iconView = NSHostingView(rootView: iconSwiftUI)
iconView.frame = NSRect(x: 0, y: 0, width: 40, height: 22)
// Creating a menu item & the menu to add them later into the status bar
//
let menuItem = NSMenuItem()
menuItem.view = contentView
let menu = NSMenu()
menu.addItem(menuItem)
// Adding content view to the status bar
//
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
statusItem.menu = menu
// Adding the status bar icon
//
statusItem.button?.addSubview(iconView)
statusItem.button?.frame = iconView.frame
// StatusItem is stored as a property.
self.statusItem = statusItem
}
// Create the status item in the Menu bar
self.statusBarItem = NSStatusBar.system.statusItem(withLength: CGFloat(NSStatusItem.variableLength))
// Add a menu and a menu item
let menu = NSMenu()
let editMenuItem = NSMenuItem()
editMenuItem.title = "Edit"
menu.addItem(editMenuItem)
//Set the menu
self.statusBarItem.menu = menu
//This is the button which appears in the Status bar
if let button = self.statusBarItem.button {
button.title = "Here"
}
3条答案
按热度按时间igsr9ssn1#
找到了一种方法来在swiftui中显示这一点,而不需要烦人的
NSPopover
。尽管我使用AppDelegate的applicationDidFinishLaunching
来执行代码,但它可以从应用的任何位置调用,即使是在SwiftUI生命周期应用中。代码如下:
bvjxkvbb2#
在AppDelegate中添加以下代码:
这会将具有自定义菜单的按钮添加到MenuBar。
编辑-如何使用SwiftUI视图
正如您所问,这里是如何使用SwiftUI视图的答案。
首先创建一个NSPopover,然后将SwiftUI视图 Package 在一个
NSHostingController
中。然后,切换弹出窗口,而不是显示NSM菜单:
执行以下操作:
yyyllmsg3#
额外菜单栏
在macOS 13.0+和Xcode 14.0+中,MenuBarExtra结构允许您创建一个系统菜单栏,类似于NSStatusBar的图标和菜单。当指定的绑定设置为
true
时,MenuBarExtra的项目将显示在系统菜单栏中。从Dock中删除应用程序图标
在Xcode的
Info
标签中,选取Application is agent (UIElement)
并将其值设定为YES
。