Search code examples
xcodemacosswiftuiwkwebviewappkit

Setting a main window title for macOS SwiftUI app in AppKit?


I have a simplistic WKWebView app that opens up a website on macOS, using SwiftUI in AppKit. However, the app window has no title - I'm talking of the top row (with the red X to close it, etc.

How do I set a title there? I've tried looking at Main.Storyboard but am not seeing anything resembling a "title segment".


Solution

  • Window is created in AppDelegate, so you can do it as below...

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Create the SwiftUI view that provides the window contents.
        let contentView = ContentView()
    
        // Create the window and set the content view. 
        window = NSWindow(
            contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered, defer: false)
        window.title = "Some title" // << assign title here
        ...