I have a SwiftUI desktop application. And I need to open a window sheet from a storyboard with a click of a button, which works. But I have a problem.
The opening window sheet is very big. Its size is 1,400 x 300 pixels. (I don't know the exact height.) I don't know where this size comes from. But I need to make it smaller. If I try to do it with the view controller, it doesn't work. How can I control the opening window sheet size?
Thank you.
The opening window sheet is very big. Its size is 1,400 x 300 pixels. (I don't know the exact height.) I don't know where this size comes from. But I need to make it smaller. If I try to do it with the view controller, it doesn't work. How can I control the opening window sheet size?
Code Block // SwiftUI View // import SwiftUI struct ContentView: View { @State private var sheetPresented = false @State private var selectionIndex = 3 var body: some View { ZStack { VStack { Button(action: { sheetPresented = true }) { Text("Show me a sheet") } .sheet(isPresented: $sheetPresented) { SheetViewControllerRepresentation(message: String(selectionIndex)) } } }.frame(minWidth: 360, idealWidth: 360, maxWidth: 360, minHeight: 240, idealHeight: 240, maxHeight: 240, alignment: .center) } } // View controller // import Cocoa import SwiftUI class SheetViewController: NSViewController { // MARK: - var message = String() // MARK: - IBOutlet @IBOutlet weak var messageLabel: NSTextField! // MARK: - IBAction @IBAction func closeClicked(_ sender: NSButton) { /* closing window */ self.view.window?.setIsVisible(false) self.view.window?.close() } // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() // Do view setup here. } override func viewWillAppear() { super.viewWillAppear() messageLabel.stringValue = message } override func viewDidAppear() { super.viewDidAppear() view.setFrameSize(CGSize(width: 320, height: 220)) } } struct SheetViewControllerRepresentation: NSViewControllerRepresentable { var message = String() func makeNSViewController(context: NSViewControllerRepresentableContext<SheetViewControllerRepresentation>) -> SheetViewController { let mainStoryboard = NSStoryboard(name: "Main", bundle: nil) let sheetViewController = mainStoryboard.instantiateController(withIdentifier: "SheetView") as! SheetViewController sheetViewController.message = self.message return sheetViewController } func updateNSViewController(_ nsViewController: SheetViewController, context: NSViewControllerRepresentableContext<SheetViewControllerRepresentation>) { } }
Thank you.
It is not clear how sheets of macOS are managed in SwiftUI.
In my test project (exactly the same code as shown), the sheet had a size 700 x 200.
I'm not sure if this works for you, but you can try setting the content size of the sheet window.
In my test project (exactly the same code as shown), the sheet had a size 700 x 200.
I'm not sure if this works for you, but you can try setting the content size of the sheet window.
Code Block override func viewWillAppear() { super.viewWillAppear() self.view.window?.setContentSize(CGSize(width: 200, height: 150)) messageLabel.stringValue = message }