macos - How to make window resizable? - Stack Overflow

I have the following super-simple app in SwiftUI on macOS.var body: some Scene {WindowGroup {Text("

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744213601a4563450.html

相关推荐

  • macos - How to make window resizable? - Stack Overflow

    I have the following super-simple app in SwiftUI on macOS.var body: some Scene {WindowGroup {Text("

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信