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 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)
本文标签: macosHow to make window resizableStack Overflow
版权声明:本文标题:macos - How to make window resizable? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744213600a2595535.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论