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 badges1 Answer
Reset to default 1From 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 thecontentSize
strategy, while all others usecontentMinSize
.
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)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744213601a4563450.html
评论列表(0条)