admin管理员组

文章数量:1399161

I have the following super-simple app in SwiftUI on macOS.

   var body: some Scene {
        WindowGroup {
           Text("test")
        }
        Settings {
            Rectangle()
                .fill(Color.red)
        }
   }

When I launch the app and open settings, the settings window is not resizable (main app window is, but Settings window isn't). I want settings window to have fixed width and resizable height. How to do it?

I have the following super-simple app in SwiftUI on macOS.

   var body: some Scene {
        WindowGroup {
           Text("test")
        }
        Settings {
            Rectangle()
                .fill(Color.red)
        }
   }

When I launch the app and open settings, the settings window is not resizable (main app window is, but Settings window isn't). I want settings window to have fixed width and resizable height. How to do it?

Share Improve this question edited Mar 25 at 7:05 DarkBee 15.5k8 gold badges72 silver badges117 bronze badges asked Mar 25 at 7:02 KavenKaven 3392 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

From the documentation of windowResizability,

The default value for all scenes if you don’t apply the modifier is automatic. With that strategy, Settings windows use the contentSize strategy, while all others use contentMinSize.

It sounds like putting .windowResizability(.contentMinSize) on a Settings scene would make it resizable in the same way as other windows. However, this doesn't actually work - the actual NSWindow still lacks the .resizable style mask. I'm not sure if this is intentional.

As a workaround, you can manually add the .resizable style mask using an NSViewRepresentable.

struct EnableWindowResize: NSViewRepresentable {
    class Helper: NSView {
        override func viewDidMoveToWindow() {
            super.viewDidMoveToWindow()
            window?.styleMask.insert(.resizable)
        }
    }
    
    func makeNSView(context: Context) -> some NSView { Helper() }
    
    func updateNSView(_ nsView: NSViewType, context: Context) { }
}

Settings {
    VStack {
        Text("Some Content")
        Spacer()
        Text("Some Other Content")
    }
    .background { EnableWindowResize() }
}
.windowResizability(.contentMinSize)

本文标签: macosHow to make window resizableStack Overflow