我正在尝试将菜单栏项添加到我的 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 中,这就是为什么我也尝试做同样的事情。
最佳答案
从您的屏幕截图来看,这似乎是您的 Mac bundle 的 AppDelegate。如果是这种情况,那么您需要删除所有 UIKit 和 #if TARGET_MAC_CATALYAT 内容,并在 AppKit 中执行所有操作。即使用NSResponder代替UIResponder等
对于示例代码,这是一个很好的开始:https://github.com/noahsark769/CatalystPlayground
它包含一个 AppKit bundle 以及用于加载它的代码。
关于ios - Mac Catalyst for Mac 菜单栏中的 NSPopover,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63800362/