You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:macOS菜单栏应用编译成功却不显示,如何解决?

解决方案

一、修复顶部菜单栏不显示的问题

你的代码缺少SwiftUI应用的入口配置,且AppDelegate的关联方式不正确,导致状态栏项未被正确初始化。按以下步骤修改:

  1. 添加@Main标记的App结构体,通过NSApplicationDelegateAdaptor关联AppDelegate:
import SwiftUI

@main
struct StatusBarApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        // 空设置场景,避免生成默认应用窗口
        Settings {
            EmptyView()
        }
    }
}

class AppDelegate: NSObject, NSApplicationDelegate {
    var statusItem: NSStatusItem!

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // 初始化状态栏项,用固定长度确保标题稳定显示
        statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
        // 设置按钮标题
        statusItem.button?.title = "TEST"
        // 可选:设置字体大小,提升文字可见性
        statusItem.button?.font = NSFont.systemFont(ofSize: 12)
    }
}
  1. 额外配置:在项目Info.plist中添加LSUIElement键,设置值为YES,隐藏应用的Dock图标,确保系统优先显示状态栏项。

二、让print语句生效

  • 在Xcode中运行应用时,print()的输出会直接显示在Xcode底部的控制台面板中,可通过点击顶部菜单栏View > Debug Area > Activate Console打开控制台查看。
  • 若需在打包后的应用中查看输出,可改用NSLog(),它会将日志写入系统日志,你可以通过Console.app(启动台>其他>控制台)搜索应用名称查看。

内容的提问来源于stack exchange,提问作者user2325445

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 17:12:22