【发布时间】:2020-12-27 05:17:34
【问题描述】:
我正在尝试将 MenuBar 项添加到我的 Mac Catalyst 应用程序。
我已成功让 iPad 应用程序在 Mac 上运行,但添加 MenuBar 项目却变得相当困难。
我已经尝试使用以下两个链接解决问题,但他们没有我可以查看的 GitHub 存储库,并且解释做了一些跳转。
https://www.highcaffeinecontent.com/blog/20190607-Beyond-the-Checkbox-with-Catalyst-and-AppKit https://developer.apple.com/documentation/xcode/creating_a_mac_version_of_your_ipad_app
现在我的 AppDelegate 中有以下代码:
#if targetEnvironment(macCatalyst)
import AppKit
import Cocoa
#endif
import UIKit
import CoreData
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
#if targetEnvironment(macCatalyst)
var popover: NSPopover!
var statusBarItem: NSStatusItem!
#endif
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
#if targetEnvironment(macCatalyst)
// Code to include from Mac.
#endif
#if targetEnvironment(macCatalyst)
let contentView = MenuBarView()
// Create the popover
let popover = NSPopover()
popover.contentSize = NSSize(width: 400, height: 500)
popover.behavior = .transient
popover.contentViewController = NSHostingController(rootView: contentView)
self.popover = popover
// Create the status item
self.statusBarItem = NSStatusBar.system.statusItem(withLength: CGFloat(NSStatusItem.variableLength))
if let button = self.statusBarItem.button {
button.image = NSImage(named: "MenuBar")
button.action = #selector(togglePopover(_:))
}
#endif
return true
}
#if targetEnvironment(macCatalyst)
@objc func togglePopover(_ sender: AnyObject?) {
if let button = self.statusBarItem.button {
if self.popover.isShown {
self.popover.performClose(sender)
} else {
self.popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
self.popover.contentViewController?.view.window?.becomeKey()
}
}
}
#endif
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
#if targetEnvironment(macCatalyst)
if let titlebar = windowScene.titlebar {
titlebar.titleVisibility = .hidden
titlebar.toolbar = nil
}
#endif
}
...more code from CoreData
显示的错误
我是 iOS 开发的新手,我真的迷路了,所以非常感谢任何帮助。
编辑
是的,截图来自 AppDelegate。
我正在尝试实现这样的东西: https://github.com/AnaghSharma/Ambar-SwiftUI
到目前为止,我看到的每个实现都将它放入 AppDelegate,这就是为什么我也尝试做同样的事情。
【问题讨论】:
-
官方还不支持,但是this dev is onto something.
-
我真的联系过他,他帮我解决了
-
解决方案是什么,是in the example您在删除的答案中提到的吗?
-
是的,但我没有删除它,它是一个模组。因为我链接到他在发布解决方案的地方创建的回购。 github.com/steventroughtonsmith/airqualitymonitor 在评论被删除之前给它加星标
标签: ios swift macos swiftui mac-catalyst