ios - Mac Catalyst for Mac 菜单栏中的 NSPopover

标签 ios swift macos swiftui mac-catalyst

我正在尝试将菜单栏项添加到我的 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

显示的错误 The Errors that is is showing

我对 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/

相关文章:

ios - 阴阳初始化 - 在调用 init 之前等待碎片

macos - 在 Mac OS X 的 XAMPP 上创建 cron(作业)

objective-c - 从 NSSplitView 的 subview 中删除蓝色轮廓

python - Scrapy Xpath 行为不一致(OS X + Linux)

ios - 快速设置模型类属性的值

ios - Xcode 7.1分配工具SIP合适

ios - SplitView Xcode7 iOS9 - iPhone 上的 DetailViewController 中缺少后退按钮

ios - 自定义 UITableViewCell 中的prepareForSegue

swift - 全局声明对象还是仅在需要时声明对象更好

ios - 不应该在其他单元格上重复的表格单元格背景颜色