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

swift - How to assign ContentView() to NSWindow? - Stack Overflow

programmeradmin3浏览0评论

I'm using Cocoa to create a window. I want to display the ContentView() in that window.

import Cocoa

@main
class AppDelegate: NSObject, NSApplicationDelegate {
    var windowX: NSWindow!
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Desired window dimensions
        let windowWidth: CGFloat = 550
        let windowHeight: CGFloat = 710
        
        // Get the screen size (optional, for centering the window)
        let screenRect = NSScreen.main?.frame ?? NSRect.zero
        let windowRect = NSRect(
            x: (screenRect.width - windowWidth) / 2,
            y: (screenRect.height - windowHeight) / 2,
            width: windowWidth,
            height: windowHeight
        )
        
        // Create the window with desired style masks
        windowX = NSWindow(
            contentRect: windowRect,
            styleMask: [.titled, .miniaturizable],
            backing: .buffered,
            defer: false
        )
        
        windowX.title = "App Window"
        
        let contentView = NSView(frame: NSRect(x: 0, y: 0, width: windowWidth, height: windowHeight))
                contentView.wantsLayer = true // Enable layer-backed view
                
        windowX.contentView = NSHostingView(rootView: ContentView())
    }
    
    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }
}

Any ideas how To make it work? When the app loads, it is just empty. There is nothing to show, or display. And it doesn't work like it is. I'm not sure why. Can you offer some advice?

I'm using Cocoa to create a window. I want to display the ContentView() in that window.

import Cocoa

@main
class AppDelegate: NSObject, NSApplicationDelegate {
    var windowX: NSWindow!
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Desired window dimensions
        let windowWidth: CGFloat = 550
        let windowHeight: CGFloat = 710
        
        // Get the screen size (optional, for centering the window)
        let screenRect = NSScreen.main?.frame ?? NSRect.zero
        let windowRect = NSRect(
            x: (screenRect.width - windowWidth) / 2,
            y: (screenRect.height - windowHeight) / 2,
            width: windowWidth,
            height: windowHeight
        )
        
        // Create the window with desired style masks
        windowX = NSWindow(
            contentRect: windowRect,
            styleMask: [.titled, .miniaturizable],
            backing: .buffered,
            defer: false
        )
        
        windowX.title = "App Window"
        
        let contentView = NSView(frame: NSRect(x: 0, y: 0, width: windowWidth, height: windowHeight))
                contentView.wantsLayer = true // Enable layer-backed view
                
        windowX.contentView = NSHostingView(rootView: ContentView())
    }
    
    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }
}

Any ideas how To make it work? When the app loads, it is just empty. There is nothing to show, or display. And it doesn't work like it is. I'm not sure why. Can you offer some advice?

Share Improve this question edited Mar 15 at 22:20 HangarRash 15.1k5 gold badges20 silver badges55 bronze badges asked Mar 15 at 21:59 MisfitMisfit 747 bronze badges 2
  • It would help to show your ContentView SwiftUI struct. Also note that your code makes no use of the contentView variable (the NSView). – HangarRash Commented Mar 15 at 22:23
  • @HangarRash the content view works perfectly. That was tested long before this change was made. I'm not sure why it doesn't load? – Misfit Commented Mar 15 at 22:32
Add a comment  | 

1 Answer 1

Reset to default 2
  1. create AppDelegate
class AppDelegate: NSObject, NSApplicationDelegate {
    var mainWnd: NSWindow? // NSWindowCust?
    private var mainWndController: NSWindowController?

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        openMainWnd()
    }
}
  1. attach appDelegate using NSApplicationDelegateAdaptor to your SwiftUI app. Also you need to create at least 1 scene. To be sure that this scene will not be shown on app start you need to create Settings scene
import SwiftUI

@main
struct FocusitoApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        Settings {
            SettingsView()
        }
    }
}
  1. in function openMainWnd() you need to
  • create instance of wnd and assign your view into it
  • save window instance into appDelegate
  • Save window controller into appDelegate

    func openMainWnd(show: Bool = true) {
        if mainWndController == nil {
            let wnd = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 200, height: 200),
                              styleMask: [.closable, .titled],
                              backing: .buffered,
                              defer: true)
            
            wnd.contentView = NSHostingView(rootView: MainView() )
            
            // all of your customization
            
            self.mainWnd = wnd
            self.mainWndController = NSWindowController(window: wnd)
        }
    }

Bonus

If you will need to customize NSWindow behavior you can use code similar to following:

class NSWindowCust: NSWindow {
    override func close() {
        //my custom logic
        super.close()
    }
}

and to use NSWindowCust in AppDelegate instead of NSWindow

发布评论

评论列表(0)

  1. 暂无评论