Home > Mobile >  Set the width of a resizable window with a Button in MacOS SwiftUI
Set the width of a resizable window with a Button in MacOS SwiftUI

Time:01-02

How can I set the width of a window that shall be resizable with a Button in MacOS SwiftUI? I tried with the frame modifier, but that sets the width permanently. I want the user to be able to drag the Sitze with the mouse and set a predefined size via a Button.

struct ResizeTest: App {
    var body: some Scene {
        WindowGroup {
          ContentView()
            .frame(minWidth: 200, maxWidth: 1000)            
            .padding(0)
        }
        .defaultSize(width: 300, height: 400)
        .windowResizability(.contentSize)
    }
}

struct ContentView: View {
  @State var width = 400.0
  
  var body: some View {
    VStack (alignment: .center) {
      Text("width: \(width)")
      Button("600"){ width = 600}
    }
    .frame(width:width )
    .background(Color.green)
  }
}

CodePudding user response:

Ok that was a little tricky ... its a bit of a hack, but it does work: The buttons set both minWidth and maxWidth to the desired value to force a resize of the window, then reset the values back to the full possible resize range (200-1000) after 0.5 secs.

@main
struct ResizeTest: App {
    var body: some Scene {
        WindowGroup {
          ContentView()
        }
        .windowResizability(.contentSize)
    }
}

struct ContentView: View {
    @State var minWidth = 200.0
    @State var maxWidth = 1000.0

    var body: some View {
        GeometryReader { geo in
            VStack (alignment: .center) {
                
                Text("Current width: \(geo.size.width.formatted())")
                    .frame(maxWidth: .infinity)
                
                Button("400"){
                    minWidth = 400 // set
                    maxWidth = 400
                    DispatchQueue.main.asyncAfter(deadline: .now()   0.5) { // reset after 0.5 secs
                        minWidth = 200
                        maxWidth = 1000
                    }
                }
                Button("600"){
                    minWidth = 600 // set
                    maxWidth = 600
                    DispatchQueue.main.asyncAfter(deadline: .now()   0.5) { // reset after 0.5 secs
                        minWidth = 200
                        maxWidth = 1000
                    }
                }
            }
        }
        .frame(height: 100)
        .frame(minWidth: minWidth, maxWidth: maxWidth)
        .background(Color.green)
    }
}
  • Related