I created a HelloWorld macOS SwiftUI project and I am seeing the option of EnterFullScreen in View menu, so how can I remove this option and disable it from bace in SwiftUI?
@main
struct testApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
CodePudding user response:
You can change this using UserDefaults
by setting the key "NSFullScreenMenuItemEverywhere" to false as in this answer but if you do it in applicationWillFinishLaunching
as in that answer it will be too late to take effect so move it to the init()
in your App struct
init() {
UserDefaults.standard.set(false, forKey: "NSFullScreenMenuItemEverywhere")
}
If you rather use the AppStorage property wrapper for this it could look like this
@AppStorage("NSFullScreenMenuItemEverywhere") var fullScreenEnabled = false
init() {
fullScreenEnabled = false
}
CodePudding user response:
The easiest way is to define a maximum size for your view, and then tell the window group to use the content size as a limit for your window's sizing.
For example:
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.frame(maxWidth: 400, maxHeight: 500)
}
.windowResizability(.contentSize)
}
}
As the window cannot now go above the size you've set, it's ineligible to be a full-screen window and SwiftUI automatically disables the option in the menu.
After a little bit of experimentation, it seems .windowResizability(.contentSize)
requires a maximum view size that's smaller than the screen – if you supply values that are larger than the current screen resolution the full screen option gets re-enabled.
You can of course omit a maximum, in which case the window will be a fixed size based on the contents.