Opening an NSViewController as a Sheet

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?

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.
Accepted Answer
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.
Code Block
override func viewWillAppear() {
super.viewWillAppear()
self.view.window?.setContentSize(CGSize(width: 200, height: 150))
messageLabel.stringValue = message
}

Excellent! Thanks a lot, OOPer. Once I close this sheet window, I cannot open it again by clicking on the 'Show me a sheet' button. Do you happen to know what I need to do? Merry Christmas to you if I don't see you again before it...

Do you happen to know what I need to do?

Your way of closing the sheet looks a little awkward to me, so tried something like this and works in my environment.
Code Block
class SheetViewController: NSViewController {
// MARK: -
var message = String()
var isPresented: Binding<Bool> = .constant(false)
// MARK: - IBOutlet
@IBOutlet weak var messageLabel: NSTextField!
// MARK: - IBAction
@IBAction func closeClicked(_ sender: NSButton) {
/* closing window */
isPresented.wrappedValue = false
}
//...
}
struct SheetViewControllerRepresentation: NSViewControllerRepresentable {
var message = String()
@Binding var isPresented: Bool
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
sheetViewController.isPresented = _isPresented //<-
return sheetViewController
}
//...
}

And use it as:
Code Block
.sheet(isPresented: $sheetPresented) {
SheetViewControllerRepresentation(message: String(selectionIndex),
isPresented: self.$sheetPresented)
}


Happy all your coming days.

Your way of closing the sheet looks a little awkward to me, so tried something like this and works in my environment.

Yes, thank you. Again, have nice holidays.
Opening an NSViewController as a Sheet
 
 
Q