Home > Mobile >  Set a maximum windowsize in a MacOS SwiftUI App
Set a maximum windowsize in a MacOS SwiftUI App

Time:01-01

I try to define the maximum windowsize in a MacOS SwiftUI App like this:

struct ContentView: View {
  @State var width = 400.0
  
  var body: some View {    
    VStack{
      GeometryReader { geometry in
        VStack {
          Text("Suggested Width : \(width)")
          Text("Width           : \(      geometry.size.width)")
          Text("Height          : \(      geometry.size.height)")
          Button("Set width to 200"){
            width = 200.0
          }
        }
        .padding()        
      }
      .frame(maxWidth: width)
    }    
    .background(Color.green)
  }
}

The .frame(maxWidth: width) only affects the "inner" green VStack.
The (MacOS)Window can be dragged bigger than the maxWidth.

How can I define the maximum Width for the (MacOS)Window?

CodePudding user response:

You can use .windowResizability in the App struct on the WindowGroup Scene.
If you set it to .contentSize it derives min and/or max values of the window size from the view sizes inside the window.

        WindowGroup {
            ContentView()
                .frame(minWidth: 100, maxWidth: 400, minHeight: 100, maxHeight: 400)
        }
        .defaultSize(width: 400, height: 400)
        .windowResizability(.contentSize) // here: this takes min and max sizes from the child view.
  • Related