最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

macOS SwiftUI toolbar not showing title and tool bar items on macOS 13 - Stack Overflow

programmeradmin2浏览0评论

I have an app that runs perfectly on macOS 14 & 15. But I would like to support macOS 13 too. But on macOS 13 the toolbar doesn't show the title and the button.

The code below is showing my mixed AppKit/SwiftUI approach. In my real app I need some more control on window management. That's why I didn't migrate to the SwiftUI App approach yet.

@main class AppDelegate: NSObject, NSApplicationDelegate {
    var window: NSWindow?

    func applicationDidFinishLaunching(_ notification: Notification) {
        let view = SideBar()
        let contentView = NSHostingView(rootView: view)
        let window = NSWindow(
            contentRect: NSRect(origin: .zero, size: CGSize(width: 500, height: 400)),
            styleMask: [.titled, .unifiedTitleAndToolbar, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered,
            defer: false
        )
        window.contentView = contentView
        window.toolbar = NSToolbar()
        window.center()
        window.makeKeyAndOrderFront(nil)
        self.window = window
    }
}

struct SideBar: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: ContentView()) {
                Text("Content")
            }
        }
    }
}

struct ContentView: View {
    var body: some View {
        HStack {
            Spacer()
            VStack {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundStyle(.tint)
                Text("Hello, world!")
            }
            Spacer()
        }
        .padding()
        .navigationTitle("Test")
        .toolbar {
            ToolbarItem(placement: .automatic) {
                Button {

                } label: {
                    Label("Add", systemImage: "plus")
                }
            }
        }
    }
}

macOS 15 (Expected)

macOS 13 (Missing toolbar title and button)

发布评论

评论列表(0)

  1. 暂无评论