xcode SwiftUI MenuBar应用程序,在@main上具有Assert

r1wp621o  于 2023-02-09  发布在  Swift
关注(0)|答案(1)|浏览(98)

我有一个SwiftUI菜单栏应用程序,启动时没有可见的窗口,除了菜单栏中的一个条目,其中有一个按钮在单击时显示视图。
这工作得很好,但偶尔,应用程序抛出Assert和菜单栏按钮没有添加,虽然它不可能点击它。
以下是不能解决问题的地方:

  • 清理构建+派生数据
  • 重启

AppDelegate中的代码(菜单栏所需)

@main // Assertion thrown here
struct MenuBarApp: App {
    
    @Environment(\.scenePhase) var scenePhase
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            // This surpresses opening an empty Window on launch
            ZStack{
                EmptyView()
            }.hidden() // removing .hidden() sometimes make the assertion go away, sometimes having .hidden() here triggers it

            // Sometimes, a second ZStack makes the assertion dissappear, sometimes it triggers it.
        }
    }
}

该Assert如下:
2022年5月24日12:58:06.263464+0200菜单栏[7119:96419]***无效_NSWindowSetFrameIvar(NSWindow *,NSRect)()中的Assert失败,NSWindow.m:935
2022-05-24 13:04:40.477258+0200菜单栏[7119:96419]无效参数不满足:〈快速用户界面。快速用户界面窗口:0x 159 e63780〉.“frame=几何图元矩形包含矩形(几何图元矩形生成((几何图元浮点数)最小整数,(几何图元浮点数)最小整数,(几何图元浮点数)最大整数-(几何图元浮点数)最小整数,(几何图元浮点数)最大整数-(几何图元浮点数)最小整数,frame)”
在其他机器上,相同代码的构建和启动运行得非常好。
在assert情况下不调用func applicationDidFinishLaunching(_ notification: Notification)
macOS 12.4, M1 Max, XCode 13.3.1

t30tvxxf

t30tvxxf1#

AppDelegate中的代码(菜单栏所需)
自Xcode 14.2、macOS 13 Ventura起,MenuBarExtra版本不再需要AppDelegateMenuBarExtra场景使用原生SwiftUI生命周期渲染系统菜单栏中的持久控件。

@main
struct MyStatubarMenuApp: App {
    var body: some Scene {
        // -- no WindowGroup required --
        // -- no AppDelegate needed --
        MenuBarExtra("😎") { 
            Text("Hello Status Bar Menu!")
        }
    }
}

相关问题