SwiftUI应用程序中的新窗口缺少窗口控制按钮
从Xcode中的一个新的SwiftuiMacOS项目开始,我设置了一些非常基本的功能SwiftUI应用程序中的新窗口缺少窗口控制按钮,swiftui,nswindow,nswindowcontroller,Swiftui,Nswindow,Nswindowcontroller,从Xcode中的一个新的SwiftuiMacOS项目开始,我设置了一些非常基本的功能 @main struct SwiftUIWindowTestApp: App { var body: some Scene { WindowGroup { ContentView() } } } //ContentView struct ContentView: View { var body: some View { //Button to open a ne
@main
struct SwiftUIWindowTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
//ContentView
struct ContentView: View {
var body: some View {
//Button to open a new window
Button("Open Preferences"){
openPreferences()
}
.padding(100)
}
//Open the new window
func openPreferences(){
let preferencesWindow = NSWindow()
preferencesWindow.contentView = NSHostingView(rootView: PreferencesView())
let controller = NSWindowController(window: preferencesWindow)
controller.showWindow(nil)
}
}
//PreferencesView
struct PreferencesView: View {
var body: some View {
Text("Preferences")
.frame(width:300, height:200)
}
}
在应用程序发布时,我看到了我的期望:
但单击“打开首选项”后,我看到一个新窗口如下所示:
为什么弹出窗口上缺少控制按钮(关闭、最小化、缩放)
我已尝试显式地将按钮设置为可见(即使它们在默认情况下是可见的),但没有任何更改:
preferencesWindow.standardWindowButton(.closeButton)?.isHidden = false
preferencesWindow.standardWindowButton(.miniaturizeButton)?.isHidden = false
preferencesWindow.standardWindowButton(.zoomButton)?.isHidden = false
我有相同的示例在AppKit应用程序中运行良好,所以SwiftUI似乎有些奇怪。有什么想法吗?看起来像
NSWindow
会创建一个这样的窗口,除非您在其上设置了宿主控制器。您的代码的此修改版本适用于我:
func openPreferences(){
let preferencesWindow = NSWindow(contentViewController: NSHostingController(rootView: PreferencesView()))
let controller = NSWindowController(window: preferencesWindow)
controller.showWindow(nil)
}