Swift MacOS上的迅捷打开新窗口
在我的MacOS SwiftUI应用程序中,我想在新窗口中显示一些视图。调用应该可以从代码的几个部分进行,所以我决定,一定要将它作为一个特殊结构的静态函数来实现。Swift MacOS上的迅捷打开新窗口,swift,macos,swiftui,appkit,Swift,Macos,Swiftui,Appkit,在我的MacOS SwiftUI应用程序中,我想在新窗口中显示一些视图。调用应该可以从代码的几个部分进行,所以我决定,一定要将它作为一个特殊结构的静态函数来实现。 很好,有什么要说的吗 struct Appwindows { static func newWindow(forSpecialView view: SpecialView, title: String = "new Window") { let newWindow = newWindowInternal
很好,有什么要说的吗
struct Appwindows {
static func newWindow(forSpecialView view: SpecialView, title: String = "new Window")
{ let newWindow = newWindowInternal(title: title)!
newWindow.contentView = NSHostingView(rootView: view)
}
private static func newWindowInternal(title: String = "new Window") -> NSWindow!
{ var newWindow: NSWindow!
newWindow = NSWindow(
contentRect: NSRect(x: 20, y: 20, width: 680, height: 600),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered,
defer: false)
newWindow.center()
newWindow.isReleasedWhenClosed = false
newWindow.title = title
newWindow.makeKeyAndOrderFront(nil)
return newWindow
}
}
是否有一种方法使其更通用,以便将任何类型的视图传递给函数?使函数通用并添加视图约束
static func newWindow<Content: View>(forSpecialView view: Content, title: String = "new Window") { // <-- Here
用法:
struct ContentView: View {
var body: some View {
Button(action: {
ContentViewNewWindow().openNewWindow()
}) {
Text("Open New Window")
}
}
}
struct ContentView: View {
var body: some View {
Button(action: {
ContentViewNewWindow().openNewWindow()
}) {
Text("Open New Window")
}
}
}