How do I disable the Show Tab Bar menu option in SwiftUI

南笙酒味 提交于 2021-02-08 09:24:07

问题


I have created a very simple app on MacOS created with SwiftUI. Default there is a menu item show tab bar. How do I remove this? I doesn't make sense to have tabs in this app.

I've found the following answering the same question, but for older versions of Swift, not for SwiftUI: How do I disable the Show Tab Bar menu option in Sierra apps?


回答1:


I was looking for an answer for this as well and found out the following:

by default - as you already mentioned - the Show/Hide Tab is active:

There is a property on NSWindow called tabbingMode which allows us to take control by setting it to .disallowed. My problem though was: in a SwiftUI 2-lifecycle app, how can I get hold of the windows of the app?

Well, there's NSApplication.shared.windows, so my first (non working!!) attempt was to modify all the windows in my @main-App struct (as I already prevented new windows from being created, that should be suffice):

import SwiftUI

@main
struct DQ_SyslogApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onAppear {
                    let _ = NSApplication.shared.windows.map { $0.tabbingMode = .disallowed }
                }
        }
        .commands {
            CommandGroup(replacing: .newItem) {} //remove "New Item"-menu entry
        }
    }
}

Unfortunately this did not work as NSApplication.shared.windows is empty in .onAppear.

My next step involved introducing an AppDelegate to my SwiftUI 2-lifecycle that implements applicationDidFinishLaunching(_:)...

class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(_ notification: Notification) {
        print("Info from `applicationDidFinishLaunching(_:): Finished launching…")
        let _ = NSApplication.shared.windows.map { $0.tabbingMode = .disallowed }
    }
}

...and introducing this AppDelegate to the app:

import SwiftUI

@main
struct DQ_SyslogApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .commands {
            CommandGroup(replacing: .newItem) {} //remove "New Item"-menu entry
        }
    }
}

This did the trick for me:

While I would prefer to have the menu entries removed entirely, this at least prevents the user from having tabs which is what the OP was asking for.

If anybody should happen to know a solution to hide those entries, please let us know. I couldn't find a CommandGroupPlacement that represents these menus...




回答2:


The equivalent in SwiftUI is the same thing as the equivalent in Swift (which is missed in that post for some reason). In your app delegate you can simply call the class method

func applicationWillFinishLaunching(_ notification: Notification) {
        NSWindow.allowsAutomaticWindowTabbing = false
}

To completely remove these items from any windows for your application.



来源:https://stackoverflow.com/questions/65460457/how-do-i-disable-the-show-tab-bar-menu-option-in-swiftui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!