NSWindowController
步骤 1: 创建 SwiftUI 视图
import SwiftUI
struct AboutView: View {
var body: some View {
VStack {
Text("About this App")
.font(.largeTitle)
.padding()
Text("This is a simple app using SwiftUI and NSWindowController.")
.padding()
}
}
}步骤 2: 创建 NSWindowController
import Cocoa
import SwiftUI
class AboutWindowController: NSWindowController {
convenience init() {
let contentView = AboutView()
let hostingView = NSHostingView(rootView: contentView)
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 400, height: 300),
styleMask: [.titled, .closable, .resizable],
backing: .buffered,
defer: false
)
window.center()
window.setFrameAutosaveName("About")
window.contentView = hostingView
self.init(window: window)
}
func showWindow(_ sender: Any?) {
super.showWindow(sender)
}
}步骤 3: 在应用中使用 NSWindowController
说明
注意事项
Last updated