Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to delete Main.storyboard in SwiftUI macOS project?

I am currently building a macOS project using SwiftUI. When I create the project, Main.storyboard file gets created automatically. I found some solution (here) to remove Main.storyboard file after setting the user interface as storyboard. But I did not find a way to delete the Main.storyboard when the user interface is SwiftUI. Any help would be appreciated! I have attached a screenshot of the macOS project with SwiftUI as the interface.

like image 984
Vijesh Rao Avatar asked Oct 27 '25 04:10

Vijesh Rao


1 Answers

Remove the main storyboard in a SwiftUI app (macOS)

Yes, here are the steps:

  1. Delete the Main.storyboard from your project (to Trash)

  2. Remove the Main reference in the General tab of your target: enter image description here

  3. On macOS there should not be any reference to the storyboard in the plist file so nothing to do, but you need to remove the @NSApplicationMain from your App Delegate: List item

  4. Now your project does not have any entry point so it will complain about a missing _main symbol. So create a main.swift file with the following content:

import AppKit

let app = NSApplication.shared
let delegate = AppDelegate()
app.delegate = delegate
app.run()

As others pointed out, you will lose the default menu of macOS (that's actually why I use this technique), so to create a menu programmatically add the following at the top of the applicationDidFinishLaunching method of your AppDelegate to add what you need:

let appMenu = NSMenuItem()
appMenu.submenu = NSMenu()
appMenu.submenu?.addItem(NSMenuItem(title: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))
let mainMenu = NSMenu(title: "My App")
mainMenu.addItem(appMenu)
NSApplication.shared.mainMenu = mainMenu

Cheers

like image 78
melMass Avatar answered Oct 29 '25 20:10

melMass