在我的MacOS SwiftUI应用程序中,我想在新的Windows中显示一些视图。调用应该可以从代码的几个部分,所以我决定,实现它作为一个静态函数的特殊结构像这样。
工作正常-有什么要说的反对它?
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
}
}
有没有一种方法可以使它更通用,以便任何类型的视图都可以传递给func?
1条答案
按热度按时间nzk0hqpo1#
使你的函数通用化并添加视图约束。
另一个好的和简单的解决方案是使用
View
扩展。用途: