I have a SwiftUI view with a button. I want to open a window sheet from a storyboard. Showing a window sheet isn't a problem. It's just that the application no longer has a menu bar. As soon as I created a storyboard and a view controller (NSViewController), the application stopped showing up properly. Now, if I try to debug it (Command + R), the window won't appear. So I have to click on the Live Preview button on the Canvas. If I do, I can click on the Bring Forward button. Finally, if I click on it, the application window always appears at the bottom-left corner of the desktop without the menu bar. So what's the issue?
Anyway, the following is my code.
Thank you.
Anyway, the following is my code.
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: - 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.